c++里面,函数strtok怎么用?
的有关信息介绍如下:
strtok: 分解字符串为一组字符串。s为要分解的字符串,delim为分隔符字符串。首次调用时,s指向要分解的字符串,之后再次调用要把s设成NULL。 功能: 分解字符串为一组字符串。s为要分解的字符串,delim为分隔符字符串。 例如:strtok("abc,def,ghi",","),最后可以分割成为abc def ghi.尤其在点分十进制的IP中提取应用较多。 函数使用: strtok函数会破坏被分解字符串的完整,调用前和调用后的s已经不一样了。如果要保持原字符串的完整,可以使用strchr和sscanf的组合等。 c #include #include int main(void) { char input[16]="abc,d"; char*p; /*strtok places a NULL terminator infront of the token,if found*/ p=strtok(input,","); if(p) printf("%s\n",p); /*Asecond call to strtok using a NULL as the first parameter returns a pointer to the character following the token*/ p=strtok(NULL,","); if(p) printf("%s\n",p); return0; } c++ #include #include using namespace std; int main() { char sentence[]="This is a sentence with 7 tokens"; cout << "The string to be tokenized is:\n" << sentence << "\n\nThe tokens are:\n\n"; char *tokenPtr=strtok(sentence,""); while(tokenPtr!=NULL) { cout< * - Added strsep() which will replace strtok() soon (because strsep() is * reentrant and should be faster). Use only strsep() in new code, please. ** * Sat Feb 09 2002, Jason Thomas , * Matthew Hawkins * - Kissed strtok() goodbye */