C庫函數(shù) char *strtok(char *str, const char *delim) 分解字符串str中的令牌使用delimitrer分隔轉(zhuǎn)換為一系列。
以下是聲明為strtok() 函數(shù)。
char *strtok(char *str, const char *delim)
src -- 這個字符串的內(nèi)容被修改,分解成較小的字符串(令牌)。
delim -- 這是C字符串,其中包含分隔符。這些可能會有所不同,從一個調(diào)用到另一個。
這個函數(shù)返回一個指針,字符串中發(fā)現(xiàn)的最后一個令牌。如果沒有令牌剩下檢索,返回空指針。
下面的例子顯示了函數(shù)strtok() 函數(shù)的用法。
#include <string.h> #include <stdio.h> int main() { const char str[80] = "This is - www.yiibai.com - website"; const char 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); }
讓我們編譯和運行上面的程序,這將產(chǎn)生以下結(jié)果:
This is www.yiibai.com website