原文:http://www.cnblogs.com/MikeZhang/archive/2012/03/24/mysplitfuncpp.html

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

一、用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 }

运行效果:

二、用STL进行字符串的分割

涉及到string类的两个函数find和substr:
1、find函数
原型:size_t find ( const string& str, size_t pos = 0 ) const;
功能:查找子字符串第一次出现的位置。
参数说明:str为子字符串,pos为初始查找位置。
返回值:找到的话返回第一次出现的位置,否则返回string::npos

2、substr函数
原型:string substr ( size_t pos = 0, size_t n = npos ) const;
功能:获得子字符串。
参数说明:pos为起始位置(默认为0),n为结束位置(默认为npos)
返回值:子字符串

实现如下:

 1 //字符串分割函数
2 std::vector<std::string> split(std::string str,std::string pattern)
3 {
4 std::string::size_type pos;
5 std::vector<std::string> result;
6 str+=pattern;//扩展字符串以方便操作
7 int size=str.size();
8
9 for(int i=0; i<size; i++)
10 {
11 pos=str.find(pattern,i);
12 if(pos<size)
13 {
14 std::string s=str.substr(i,pos-i);
15 result.push_back(s);
16 i=pos+pattern.size()-1;
17 }
18 }
19 return result;
20 }

完整代码:

 1 /*
2 File : split1.cpp
3 Author : Mike
4 E-Mail : Mike_Zhang@live.com
5 */
6 #include <iostream>
7 #include <string>
8 #include <vector>
9
10 //字符串分割函数
11 std::vector<std::string> split(std::string str,std::string pattern)
12 {
13 std::string::size_type pos;
14 std::vector<std::string> result;
15 str+=pattern;//扩展字符串以方便操作
16 int size=str.size();
17
18 for(int i=0; i<size; i++)
19 {
20 pos=str.find(pattern,i);
21 if(pos<size)
22 {
23 std::string s=str.substr(i,pos-i);
24 result.push_back(s);
25 i=pos+pattern.size()-1;
26 }
27 }
28 return result;
29 }
30
31 int main()
32 {
33 std::string str;
34 std::cout<<"Please input str:"<<std::endl;
35 //std::cin>>str;
36 getline(std::cin,str);
37 std::string pattern;
38 std::cout<<"Please input pattern:"<<std::endl;
39 //std::cin>>pattern;
40 getline(std::cin,pattern);//用于获取含空格的字符串
41 std::vector<std::string> result=split(str,pattern);
42 std::cout<<"The result:"<<std::endl;
43 for(int i=0; i<result.size(); i++)
44 {
45 std::cout<<result[i]<<std::endl;
46 }
47
48 std::cin.get();
49 std::cin.get();
50 return 0;
51 }

运行效果:

三、用Boost进行字符串的分割

用boost库的正则表达式实现字符串分割
实现如下:

 1 std::vector<std::string> split(std::string str,std::string s)
2 {
3 boost::regex reg(s.c_str());
4 std::vector<std::string> vec;
5 boost::sregex_token_iterator it(str.begin(),str.end(),reg,-1);
6 boost::sregex_token_iterator end;
7 while(it!=end)
8 {
9 vec.push_back(*it++);
10 }
11 return vec;
12 }

完整代码:

运行效果:

补充:

最近发现boost里面有自带的split的函数,如果用boost的话,还是直接用split的好,这里就不多说了,代码如下:

#include <iostream>
#include <string>
#include <vector>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/split.hpp> using namespace std; int main()
{
string s = "sss/ddd,ggg";
vector<string> vStr;
boost::split( vStr, s, boost::is_any_of( ",/" ), boost::token_compress_on );
for( vector<string>::iterator it = vStr.begin(); it != vStr.end(); ++ it )
cout << *it << endl;
return 0;
}

好,就这些了,希望对你有帮助。

E-Mail : Mike_Zhang@live.com

【转】字符串分割(C++)的更多相关文章

  1. SQL Server 游标运用:鼠标轨迹字符串分割

    一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 游标模板(Cursor Template) 鼠标轨迹字符串分割SQL脚本实现(SQL Code ...

  2. Oracle 超长字符串分割劈分

    Oracle 超长字符串分割劈分,具体能有多长没测过,反正很大.... 下面,,,,直奔主题了: CREATE OR REPLACE FUNCTION splitstr(p_string IN clo ...

  3. php学习零散笔记—字符串分割、fetch函数和单双引号。

    1 字符串分割——split()函数和preg_split()函数 split — 用正则表达式将字符串分割到数组中——貌似PHP5.3以上已不赞成使用 array split ( string $p ...

  4. 工作中用到的oracle字符串分割整理

    oracle部分: 定义类型(用于字符串分割): create or replace TYPE "STR_SPLIT" IS TABLE OF VARCHAR2 (4000); 字 ...

  5. Python 字符串分割的方法

    在平时工作的时候,发现对于字符串分割的方法用的比较多,下面对分割字符串方法进行总结一下:第一种:split()函数split()函数应该说是分割字符串使用最多的函数用法:str.split('分割符' ...

  6. 在C++中实现字符串分割--split

    字符串分割 在一些比较流行的语言中,字符串分割是一个比较重要的方法,不论是在python,java这样的系统级语言还是js这样的前端脚本都会在用到字符串的分割,然而在c++中却没有这样的方法用来调用. ...

  7. 随笔 JS 字符串 分割成字符串数组 并动态添加到指定ID的DOM 里

    JS /* * 字符串 分割成字符串数组 并动态添加到指定ID的DOM 里 * @id 要插入到DOM元素的ID * * 输入值为图片URL 字符串 * */ function addImages(i ...

  8. js 字符串分割成字符串数组 遍历数组插入指定DOM里 原生JS效果

    使用的TP3.2 JS字符串分割成字符串数组 var images='{$content.pictureurl} ' ;结构是这样 attachment/picture/uploadify/20141 ...

  9. oracle根据分隔符将字符串分割成数组函数

    --创建表类型 create or replace type mytype as table of number;--如果定义成varchar--CREATE OR REPLACE type myty ...

  10. hive函数 -- split 字符串分割函数

    hive字符串分割函数 split(str, regex) - Splits str around occurances that match regexTime taken: 0.769 secon ...

随机推荐

  1. 真机无法接收到android.provider.Telephony.SMS_RECEIVED的问题

    小米2S有一个设定会导致,App无法接收到android.provider.Telephony.SMS_RECEIVED广播. 解决方案 关闭系统短信优先设置 短信-设置-高级设置-系统短信优先   ...

  2. 根据权限显示隐藏SharePoint 2010快速启动栏的链接

    转:http://www.360sps.com/Item/ShowAndHiddenLink.aspx 在SharePoint 2010的快速启动栏中可以根据权限来显示或隐藏列表.库.网站的链接,如果 ...

  3. 基于WebForm+EasyUI的业务管理系统形成之旅 -- ParamQueryGrid行、列合并(Ⅸ)

    上篇<基于WebForm+EasyUI的业务管理系统形成之旅 -- 施工计划查询(Ⅷ)>,主要介绍通过报表工具数据钻取,获取施工计划详细信息. 这篇我们看看ParamQueryGrid[行 ...

  4. 记录一次Jmeter性能测试

    一.引言 之前有总结过如何写Java请求测试用例类,写完测试脚本调通之后,信心满满地以为我准备好可以开始性能测试了.结果在评审测试计划的时候,当即被项目组狠狠的扇了一耳光,各种不确定的点:性能指标不明 ...

  5. 移动端混合型App(hybrid app)自动化测试选型与实践

    背景 公司产品的业务已经发展到了移动端,开发选型已经结束,决定使用phonegap做移动端的web应用开发平台.考虑到业务的复杂与多样,移动端的测试同样需要自动化.在网上看了很多,最终锁定了3个移动端 ...

  6. QTP、LoadRunner、QC工具下载地址

    QTP10.0工具下载地址:http://h30302.www3.hp.com/prdownloads/T6510-15063.zip?ordernumber=380454070&itemid ...

  7. js特效第九天

    offset家族(获取元素尺寸) offsetWidth得到对象的宽度,自己的,与他人无关 offsetWidth = width+border+padding,不包含margin offsetHei ...

  8. 4.2 CUDA Reduction 一步一步优化

    Reduction并行分析: 每个线程是基于一个树状的访问模型,从上至下,上一层读取数据相加得到下一层的数据.不停的迭代,直到访问完所有的数据. 利用这么多的线程块(thread block)我们需要 ...

  9. 5.2 CUDA Histogram直方图

    什么是Histogramming Histogramming是一种从大的数据集中提取典型特征和模式的方式. 在统计学中,直方图(英语:Histogram)是一种对数据分布情况的图形表示,是一种二维统计 ...

  10. hive 0.11的安装配置

    一.上传hive 0.11解压后的文件到linux 1.用的版本是shark站点提供的,可能是针对shark修改了代码. 2.追加mysql.oracle两个jdbc驱动包到lib目录下. 二.配置相 ...