strtok() – C语言库函数
C库函数 char *strtok(char *str, const char *delim) 分解字符串str中的令牌使用delimitrer分隔转换为一系列。
声明
以下是声明为strtok() 函数。
char*strtok(char*str,constchar*delim)
参数
-
src -- 这个字符串的内容被修改,分解成较小的字符串(令牌)。
-
delim -- 这是C字符串,其中包含分隔符。这些可能会有所不同,从一个调用到另一个。
返回值
这个函数返回一个指针,字符串中发现的最后一个令牌。如果没有令牌剩下检索,返回空指针。
例子
下面的例子显示了函数strtok() 函数的用法。
#include<string.h>#include<stdio.h>int main(){constchar str[80]="This is - www.xyhtml5.com - website";constchar s[2]="-";char*token;/* get the first token */ token = strtok(str, s);/* walk through other tokens */while( token != NULL ){ printf(" %s ", token ); token = strtok(NULL, s);}return(0);}
让我们编译和运行上面的程序,这将产生以下结果:
This is www.xyhtml5.com website