版权声明:本文为博主原创文章,未经博主允许不得转载。
c 语言下的字符串分割函数:
一、strtok():
原型:char *strtok(char s[], const char *delim);
分解字符串为一组字符串。s为要分解的字符串,delim为分隔符字符串
strtok函数会破坏被分解字符串的完整,调用前和调用后的s已经不一样了。
一个列子:
当有连续多个分隔符时,都只会分割一次。
输出:
192 168 9 14
192
再看一个例子:
输出:
以下文档加粗部分的说明。
strtok
char * strtok ( char * str, const char * delimiters );
On a first call, the function expects a C string as argument for str, whose first character is used as the starting location to scan for tokens. In subsequent calls, the function expects a null pointer and uses the position right after the end of last token as the new starting location for scanning.
To determine the beginning and the end of a token, the function first scans from the starting location for the first character not contained indelimiters (which becomes thebeginning of the token). And then scans starting from thisbeginning of the token for the first character contained indelimiters, which becomes theend of the token.
This end of the token is automatically replaced by a null-character by the function, and the beginning of the token is returned by the function.
Once the terminating null character of str has been found in a call to strtok, all subsequent calls to this function with a null pointer as the first argument return a null pointer.
The point where the last token was found is kept internally by the function to be used on the next call (particular library implementations are not required to avoid data races).
二、strtok_s 和 strtok_r
strtok()是一个不安全的函数,因为其把剩余的子串存放到一个静态变量里,若多线程同时访问,则会出现错误。
所以,在windows下出现了strtok_s:
char *strtok_s( char *strToken, const char *strDelimit, char **buf);
在Linux下,有strtok_r()函数。
输出:
三、strsep
在linux下,有更快的strsep()函数替代strtok()。
char *strsep(char ** str,const char * del);
C语言:
strsep()不需要判断token的开始字符。
输出:
//上面的输出显示有问题,可以点view plain查看
应该是
02.168..1....112 //最前面有一个空格
可见,函数改变了原字符串,所以函数的参数不能是常量字符串。
如:
char *str = "zcxwhimxwad";
strsep(&str,"mx");
运行时会出现段错误。
还有就是分割时,分隔符的匹配问题,"mx"表示匹配m或x。
总结一下:
strtok(),函数是一个线程不安全的函数,多线程时,windows下可以用strtok_s代替,linux下可以用strtok_r代替。在linux下,有一个更加快速的strsep()函数可以代替strtok.
strtok()函数使用时原字符串变量已被更改,分割成的token的开始字符是不包含在delimiter中的第一个字符。strtok_s和strtok_r和strtok用法相似。
strsep()函数不需要像strtok一样判断开始字符。