1. 函数定义
char * strtok ( char * str, const char * delimiters )
将str按照delimiters定义的分割符来进行分割。
初次使用时,需要传入str,将str的首个字符位置作为查找的起始位置,并返回不包含dilimiters定义字符的子串;后续使用传入NULL,并使用上一次查找到子串的尾部位置的下一个位置作为查找起始位置,继续查找。
2. 例子代码
/* strtok example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] ="- This, a sample string.";
char * pch;
printf ("Splitting string \"%s\" into tokens:\n",str);
pch = strtok (str," ,.-");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ,.-");
}
return 0;
}
输出为:
Splitting string "- This, a sample string." into tokens:
This
a
sample
string