怎样用C\C++函数分割字符串

如题所述

其实,用C\C++函数分割字符串的方法有很多种,下面给你分享其中一种方法:用strtok函数进行字符串分割

原型: char *strtok(char *str, const char *delim);

功能:分解字符串为一组字符串。

参数说明:str为要分解的字符串,delim为分隔符字符串。

返回值:从str开头开始的一个个被分割的串。当没有被分割的串时则返回NULL。

其它:strtok函数线程不安全,可以使用strtok_r替代。

示例:

//借助strtok实现split

#include

#include

int main()

{

char s[] = "Golden Global View,disk * desk";

const char *d = " ,*";

char *p;

p = strtok(s,d);

while(p)

{

printf("%s\n",p);

p=strtok(NULL,d);

}

return 0;

}

运行效果如下图所示:

温馨提示:答案为网友推荐,仅供参考
相似回答