strtok
语法:
1 2 3 |
? #include <string.h> ? char *strtok( char *str1, const char *str2 ); |
功能:函数返回字符串str1中紧接“标记”的部分的指针, 字符串str2是作为标记的分隔符。如果分隔标记没有找到,函数返回NULL。为了将字符串转换成标记,第一次调用str1 指向作为标记的分隔符。之后所以的调用str1 都应为NULL。
例如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
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 |