C++ 编程/代码/标准 C 库/函数/strtok
外观
语法 |
#include <cstring>
char *strtok( char *str1, const char *str2 );
|
strtok() 函数返回 str1 中下一个 "token" 的指针,其中 str2 包含确定 token 的分隔符。如果未找到 token,strtok() 返回 NULL。为了将字符串转换为 token,对 strtok() 的第一次调用应让 str1 指向要标记化的字符串。所有后续调用应让 str1 为 NULL。
例如
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"