这个函数好用的很, 可以使用某固定字符分割字符串。MSDN上的事例如下:
Example
/* STRTOK.C: In this program, a loop uses strtok
* to print all the tokens (separated by commas
* or blanks) in the string named "string".
*/
#include <string.h>
#include <stdio.h>
char string[] = "A string/tof ,,tokens/nand some more tokens";
char seps[] = " ,/t/n";
char *token;
void main( void )
{
printf( "%s/n/nTokens:/n", string );
/* Establish string and get the first token: */
token = strtok( string, seps );
while( token != NULL )
{
/* While there are tokens in "string" */
printf( " %s/n", token );
/* Get next token: */
token = strtok( NULL, seps );
}
}
Output
A string of ,,tokens
and some more tokens
Tokens:
A
string
of
tokens
and
some
more
tokens
本文介绍了一个实用的C语言函数strtok,该函数能够通过指定的分隔符将字符串分割成多个子串。通过示例代码展示了如何使用strtok从包含多种分隔符的字符串中提取独立的词汇。
491

被折叠的 条评论
为什么被折叠?



