一、头文件:#include <string.h>
二、函数原型:char * strtok (char *str, const char * delimiters);
三、参数:str,待分割的c风格的字符串(c-string);delimiters,分割符字符串。
四、说明:
将字符串str分解成若干个单词,单词之间以delimiters字符串中的任一一个字符分割。第一次调用strtok时,str应该是一个c风格的字符串(c-string),随后的调用中, str应该是一个NULL指针。
五、例子:
[cpp] view plaincopy
- /* 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;
- }
Output:
[cpp] view plaincopy
- Splitting string "- This, a sample string." into tokens:
- This
- a
- sample
- string
时间: 2024-10-07 04:56:56