跳转到内容

C++ 编程/代码/标准 C 库/函数/strtok

来自维基教科书,自由的教学资源
语法
#include <cstring>
char *strtok( char *str1, const char *str2 );

strtok() 函数返回 str1 中下一个 "token" 的指针,其中 str2 包含确定 token 的分隔符。如果未找到 token,strtok() 返回 NULL。为了将字符串转换为 token,对 strtok() 的第一次调用应让 str1 指向要标记化的字符串。所有后续调用应让 str1NULL

例如

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" 
相关主题
strchr - strcspn - strpbrk - strrchr - strspn - strstr
华夏公益教科书