字符串分割(C++)

经常碰到字符串分割的问题,这里总结下,也方便我以后使用。

一、用strtok函数进行字符串分割

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

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

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

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

示例:

复制代码
 1 //借助strtok实现split
2 #include <string.h>
3 #include <stdio.h>
4
5 int main()
6 {
7 char s[] = "Golden Global View,disk * desk";
8 const char *d = " ,*";
9 char *p;
10 p = strtok(s,d);
11 while(p)
12 {
13 printf("%s\n",p);
14 p=strtok(NULL,d);
15 }
16
17 return 0;
18 }
复制代码

 运行效果: 

更详细:http://www.cnblogs.com/MikeZhang/archive/2012/03/24/MySplitFunCPP.html

posted @ 2013-02-18 11:24  stma  阅读(2644)  评论(0)    收藏  举报