关于标准c中的strtok和linux库里面的strsep
2012-02-13 21:07
253 查看
转载自:http://snprintf.blog.51cto.com/3676303/695760
strsep
函数原型:
需要注意:
1、 被分割字串要被改变,所以不能操作存放在静态存储区的字串常量。
2、 分割符要被替换成’\0’。
3、 需要传二级指针,因为s1是指向分割字串,第一次指向源字串,调用后指向分割后的下一个token。所以s1会改变,需要传递二级指针。
对于注意1,常犯错误如下:
str指向的字串是静态存储区,属于字符串常量,不能修改。会出现段错误。
而对于注意3,常犯错误如下:
&str是一级指针,所以也会出现段错误。
其实对于会修改源字串的函数都容易出现上面的错误。用时要格外小心注意。
strtok也跟strsep一样,是用来分割字符串的,但不同的是strtok把待分割字串和遍历指针分开,并且遍历指针也是内置的。但是strsep把待分割字串和内置指针整合,所以需要传二级指针。
函数原型:
同样注意上面的错误地方。
strtok要对同一字串进行多次分割,第一次需要指定源字串,接下来需要传NULL;而strsep则不需关心这个问题,因为它是把待分割字串的二级指针传过去,内部会进行指针的后移。
strsep使用如下:
而strtok使用如下:
第一次要指定待分割字串,接下来则要传NULL。
转载自:http://blog.csdn.net/yafeng_jiang/article/details/7109285
函数原型:char *strtok(char *s, const char *delim);
char *strsep(char **s, const char *delim);
功能:strtok和strsep两个函数的功能都是用来分解字符串为一组字符串。s为要分解的字符串,delim为分隔符字符串。
返回值:从s开头开始的一个个子串,当没有分割的子串时返回NULL。
相同点:两者都会改变源字符串,想要避免,可以使用strdupa(由allocate函数实现)或strdup(由malloc函数实现)。
strtok函数第一次调用时会把s字符串中所有在delim中出现的字符替换为NULL。然后通过依次调用strtok(NULL, delim)得到各部分子串。
测试代码:
[cpp]
view plaincopy
#include <stdio.h>
#include <string.h>
int main(void) {
char s[] = "hello, world! welcome to china!";
char delim[] = " ,!";
char *token;
for(token = strtok(s, delim); token != NULL; token = strtok(NULL, delim)) {
printf(token);
printf("+");
}
printf("\n");
return 0;
}
输出结果为:hello+world+welcome+china+
对于strsep有如下例子:
[cpp]
view plaincopy
#include <stdio.h>
#include <string.h>
int main(void) {
char source[] = "hello, world! welcome to china!";
char delim[] = " ,!";
char *s = strdup(source);
char *token;
for(token = strsep(&s, delim); token != NULL; token = strsep(&s, delim)) {
printf(token);
printf("+");
}
printf("\n");
return 0;
}
输出结果为:hello++world++welcome+to+china++
为什么用strtok时子串中间只有一个“+”,而strsep却有多个"+"呢?文档中有如下的解释:
One difference between strsep and strtok_r is that if the input string contains more
than one character from delimiter in a row strsep returns an empty string for each
pair of characters from delimiter. This means that a program normally should test
for strsep returning an empty string before processing it.
大意是:如果输入的串的有连续的多个字符属于delim,(此例source中的逗号+空格,感叹号+空格等就是这种情况),strtok会返回NULL,而strsep会返回空串""。因而我们如果想用strsep函数分割字符串必须进行返回值是否是空串的判断。这也就解释了strsep的例子中有多个"+"的原因。
我们在自己的程序中最好尽量避免使用strtok,转而使用strsep。
下面的说明摘自于最新的Linux内核2.6.29,说明了strtok()已经不再使用,由速度更快的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 <asm-xx/string.h>
* These are buggy as well..
* * Fri Jun 25 1999, Ingo Oeser <ioe@informatik.tu-chemnitz.de>
* - 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 <jason@topic.com.au>,
* Matthew Hawkins <matt@mh.dropbear.id.au>
* - Kissed strtok() goodbye
*/
strsep
函数原型:
Char * strsep(char **s1, const char *delimt);
需要注意:
1、 被分割字串要被改变,所以不能操作存放在静态存储区的字串常量。
2、 分割符要被替换成’\0’。
3、 需要传二级指针,因为s1是指向分割字串,第一次指向源字串,调用后指向分割后的下一个token。所以s1会改变,需要传递二级指针。
对于注意1,常犯错误如下:
char *str = "This is a example to test the function of strsep"; strsep(&str, " ");
str指向的字串是静态存储区,属于字符串常量,不能修改。会出现段错误。
而对于注意3,常犯错误如下:
char str[] = "This is a example to test the function of strsep"; strsep(&str, " ");
&str是一级指针,所以也会出现段错误。
其实对于会修改源字串的函数都容易出现上面的错误。用时要格外小心注意。
strtok也跟strsep一样,是用来分割字符串的,但不同的是strtok把待分割字串和遍历指针分开,并且遍历指针也是内置的。但是strsep把待分割字串和内置指针整合,所以需要传二级指针。
函数原型:
char *strtok(char *s1, const char *delim);
同样注意上面的错误地方。
strtok要对同一字串进行多次分割,第一次需要指定源字串,接下来需要传NULL;而strsep则不需关心这个问题,因为它是把待分割字串的二级指针传过去,内部会进行指针的后移。
strsep使用如下:
char *str = strdup("This is a example to test the function of strsep"); char *p = NULL; while(NULL != ( p = strsep(&str, " ")) { puts(p); }
而strtok使用如下:
第一次要指定待分割字串,接下来则要传NULL。
char *str = strdup("This is a example to test the function of strsep"); char *p = NULL; char *tmp = str; while (NULL != (p = strsep(tmp, " "))) { puts(p); tmp = NULL; }
转载自:http://blog.csdn.net/yafeng_jiang/article/details/7109285
函数原型:char *strtok(char *s, const char *delim);
char *strsep(char **s, const char *delim);
功能:strtok和strsep两个函数的功能都是用来分解字符串为一组字符串。s为要分解的字符串,delim为分隔符字符串。
返回值:从s开头开始的一个个子串,当没有分割的子串时返回NULL。
相同点:两者都会改变源字符串,想要避免,可以使用strdupa(由allocate函数实现)或strdup(由malloc函数实现)。
strtok函数第一次调用时会把s字符串中所有在delim中出现的字符替换为NULL。然后通过依次调用strtok(NULL, delim)得到各部分子串。
测试代码:
[cpp]
view plaincopy
#include <stdio.h>
#include <string.h>
int main(void) {
char s[] = "hello, world! welcome to china!";
char delim[] = " ,!";
char *token;
for(token = strtok(s, delim); token != NULL; token = strtok(NULL, delim)) {
printf(token);
printf("+");
}
printf("\n");
return 0;
}
输出结果为:hello+world+welcome+china+
对于strsep有如下例子:
[cpp]
view plaincopy
#include <stdio.h>
#include <string.h>
int main(void) {
char source[] = "hello, world! welcome to china!";
char delim[] = " ,!";
char *s = strdup(source);
char *token;
for(token = strsep(&s, delim); token != NULL; token = strsep(&s, delim)) {
printf(token);
printf("+");
}
printf("\n");
return 0;
}
输出结果为:hello++world++welcome+to+china++
为什么用strtok时子串中间只有一个“+”,而strsep却有多个"+"呢?文档中有如下的解释:
One difference between strsep and strtok_r is that if the input string contains more
than one character from delimiter in a row strsep returns an empty string for each
pair of characters from delimiter. This means that a program normally should test
for strsep returning an empty string before processing it.
大意是:如果输入的串的有连续的多个字符属于delim,(此例source中的逗号+空格,感叹号+空格等就是这种情况),strtok会返回NULL,而strsep会返回空串""。因而我们如果想用strsep函数分割字符串必须进行返回值是否是空串的判断。这也就解释了strsep的例子中有多个"+"的原因。
我们在自己的程序中最好尽量避免使用strtok,转而使用strsep。
下面的说明摘自于最新的Linux内核2.6.29,说明了strtok()已经不再使用,由速度更快的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 <asm-xx/string.h>
* These are buggy as well..
* * Fri Jun 25 1999, Ingo Oeser <ioe@informatik.tu-chemnitz.de>
* - 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 <jason@topic.com.au>,
* Matthew Hawkins <matt@mh.dropbear.id.au>
* - Kissed strtok() goodbye
*/
相关文章推荐
- 关于标准c中的strtok和linux库里面的strsep
- linux中关于标准输入中涉及EOF的处理方式
- linux 关于session缓存丢失,自己掉坑里面了
- 关于linux里面编程语句while的运用,转载
- 关于操作系统里面的P-V操作的信号量跟Linux下的无名信号量的区别!
- linux 关于标准输入输出错误 STDIN STDOUT STDEER
- UNIX里面关于标准IO的几种缓冲机制
- Linux与Windows关于标准输入中涉及EOF的处理方式
- 这里面有好多关于meego、mer的包,包括很多标准的armv7hl包
- 关于linux下标准读入read和fgets和scanf的区别
- Linux与Windows关于标准输入中涉及EOF的处理方式
- 关于Linux3.0驱动里面是否需要关中断的探讨
- 关于Linux下的dz里面的rewrite的步骤。
- 关于LiPS release的所谓标准:Linux电话终端标准1.0 的批判
- 关于Linux网络安全的内在限制
- 关于linux特殊重定向的理解
- JAVA里面关于byte数组和String之间的转换问题
- Linux 全球统一标准即将出台 避免走 Unix 老路
- 关于arm linux initrd boot 探讨
- linux下关于execve函数的简单解析