Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1220068
  • 博文数量: 261
  • 博客积分: 4196
  • 博客等级: 上校
  • 技术积分: 3410
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-17 17:05
文章分类

全部博文(261)

文章存档

2018年(1)

2017年(22)

2016年(2)

2015年(8)

2014年(27)

2013年(40)

2012年(161)

分类: LINUX

2012-03-19 12:51:57

一、分割字符 strtok
      char *strtok(char *s, const char *delim);
      功能:分解字符串为一组字符串。s为要分解的字符串,delim为分隔符字符串。
      说明:strtok()用来将字符串分割成一个个片段。参数s指向欲分割的字符串,参数delim则为分割字符串,当strtok()在参数s的字符串中发现到 参数delim的分割字符时则会将该字符改为\0 字符。在第一次调用时,strtok()必需给予参数s字符串,往后的调用则将参数s设置成NULL。每次调用成功则返回被分割出片段的指针。
      返回值:从s开头开始的一个个被分割的串。当没有被分割的串时则返回NULL。   所有delim中包含的字符都会被滤掉,并将被滤掉的地方设为一处分割的节点。
      范例:
#include
#include
main()
{
   char s[]="ab-cd: ef;gh: i-jkl;mnop;qrs_tu:vwx-y;z";
   char *delim="-:";
   char *p;
   printf("%s ",strtok(s,delim));
   while((p=strtok(NULL,delim))) printf("%s",p);
   printf("\n");
}

下面的说明摘自于最新的2.6.29,说明了这个函数已经不再使用,由速度更快的strsep()代替   
/*   
* linux/lib/string.c   *   
* Copyright (C) 1991, 1992 Linus Torvalds   */   
/*   
* stupid library routines.. The optimized versions should generally be found   
* as inline code in   *   
* These are buggy as well..   *   
* * Fri Jun 25 1999, Ingo Oeser   
* - Added strsep() which will replace strtok() soon (because strsep() is   
* reentrant and should be faster). Use only strsep() in new code, please.   *   
* * Sat Feb 09 2002, Jason Thomas ,   
* Matthew Hawkins   
* - Kissed strtok() goodbye   
*/   
#include   
#include   
int main(void)   
{   
char input[16]="a,b,c,d";   
char *p;   
p=strtok(input,",");   
while(p)   
{   printf("%s\n",p);
   p=strtok(NULL,",");   
}   
return 0;   
}

二、strstr
     包含文件:string.h
   函数名: strstr
   函数原型:extern char *strstr(char *str1, char *str2);
   功能:找出str2字符串在str1字符串中第一次出现的位置(不包括str2的串结束符)。
   返回值:返回该位置的指针,如找不到,返回空指针

阅读(803) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~