首页 > 编程知识 正文

strtok_s函数,strtok strsep

时间:2023-05-04 09:19:09 阅读:241758 作者:662

原文转自: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)得到各部分子串。

#include <string>#include <stdio.h> int main(int arg, const char *argv[]){ char* string = strdup( "/home/yinlijun/project:/home/yinlijun:/home/someone"); /*字符串不能为常量,所以strdup*/ char* p; while((p = strsep(&string, ":")) != NULL) /*第一个参数设为二级指针, 字符串中所有的第二个参数(子串)最后会被替代成‘/0‘*/ { printf("%s/n", p); } return 0;}

#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有如下例子:

#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。

PS:因为函数内部会修改原字符串变量,所以传入的参数不能是不可变字符串(即文字常量区)。

如 char *tokenremain ="abcdefghij"//编译时为文字常量,不可修改。

strtok(tokenremain,"cde");

strsep(&tokenremain,"cde");

编译通过,运行时会报段错误。

PS:找个库函数源码的在线查询网站真不容易,先找到了这个http://sources.redhat.com/cgi-bin/cvsweb.cgi/libc/?cvsroot=glibc

之后,发现了经常去找软件的oschina有源码库,真是踏破铁鞋无觅处,得来全不费工夫!

http://www.oschina.net/code/explore/glibc-2.9/string/strtok.c


char str[] = "now # is the time for all # good men to come to the # aid of their country"; char delims[] = "#"; char *result = NULL; result = strtok( str, delims ); while( result != NULL ){ printf( "result is "%s"n", result ); result = strtok( NULL, delims ); } 结果:

result is "now "  result is " is the time for all "  result is " good men to come to the "  result is " aid of their country"   

char str2[] = "2011/11/28"; char *buf; char *token; buf = str2; while((token = strsep(&buf, "/")) != NULL){ printf("%sn", token); }

输出:

2011  11  28  

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。