C++标准库里面没有字符分割函数split ,这可太不方便了,我已经遇到>3次如何对字符串快速分割这个问题了。列几个常用方法以备不时之需。

方法一: 利用STL自己实现split 函数(常用,简单,直观)

原型: vector<string> split(const string &s, const string &seperator);

输入一个字符串,一个分隔符字符串(可包含多个分隔符),返回一个字符串向量。这是我最喜欢的方法,因为它最直观,在平常也最常用。实现及测试代码如下

#include <vector>
#include <string>
#include <iostream>
using namespace std; vector<string> split(const string &s, const string &seperator){
vector<string> result;
typedef string::size_type string_size;
string_size i = ; while(i != s.size()){
//找到字符串中首个不等于分隔符的字母;
int flag = ;
while(i != s.size() && flag == ){
flag = ;
for(string_size x = ; x < seperator.size(); ++x)
  if(s[i] == seperator[x]){
  ++i;
  flag = ;
   break;
  }
} //找到又一个分隔符,将两个分隔符之间的字符串取出;
flag = ;
string_size j = i;
while(j != s.size() && flag == ){
for(string_size x = ; x < seperator.size(); ++x)
  if(s[j] == seperator[x]){
  flag = ;
   break;
  }
if(flag == )
  ++j;
}
if(i != j){
result.push_back(s.substr(i, j-i));
i = j;
}
}
return result;
} int main(){
string s = "a,b*c*d,e";
vector<string> v = split(s, ",*"); //可按多个字符来分隔;
for(vector<string>::size_type i = ; i != v.size(); ++i)
cout << v[i] << " ";
cout << endl;
//输出: a b c d
}

@egmkang 提供了一段更简洁高效的代码,实现如下:

void SplitString(const std::string& s, std::vector<std::string>& v, const std::string& c)
{
std::string::size_type pos1, pos2;
pos2 = s.find(c);
pos1 = ;
while(std::string::npos != pos2)
{
v.push_back(s.substr(pos1, pos2-pos1)); pos1 = pos2 + c.size();
pos2 = s.find(c, pos1);
}
if(pos1 != s.length())
v.push_back(s.substr(pos1));
}

方法二: 用C语言中的strtok 函数来进行分割

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

strtok函数包含在头文件<string.h>中,对于字符数组可以采用这种方法处理。当然也可以将字符数组转换成字符串之后再使用法一。测试代码如下

#include <string.h>
#include <stdio.h> int main(){
char s[] = "a,b*c,d";
const char *sep = ",*"; //可按多个字符来分割
char *p;
p = strtok(s, sep);
while(p){
printf("%s ", p);
p = strtok(NULL, sep);
}
printf("\n");
return ;
}
//输出: a b c d

方法三: boost库中包含了split 函数

boost库有很多方法来实现split,也包含了一个split函数,可以直接使用,非常实用而且强大,但是得自己下载boost库。使用代码如下

#include <boost/algorithm/string.hpp>
#include <iostream>
#include <string>
#include <vector> using namespace std;
using namespace boost; void print( vector <string> & v )
{
for (size_t n = ; n < v.size(); n++)
cout << "\"" << v[ n ] << "\"\n";
cout << endl;
} int main()
{
string s = "a,b, c ,,e,f,";
vector <string> fields; cout << "Original = \"" << s << "\"\n\n"; cout << "Split on \',\' only\n";
split( fields, s, is_any_of( "," ) );
print( fields ); cout << "Split on \" ,\"\n";
split( fields, s, is_any_of( " ," ) );
print( fields ); cout << "Split on \" ,\" and elide delimiters\n";
split( fields, s, is_any_of( " ," ), token_compress_on );
print( fields ); return ;
}

输出结果如下:

Original = "a,b, c ,,e,f,"

Split on ',' only
"a"
"b"
" c "
""
"e"
"f"
"" Split on " ,"
"a"
"b"
""
"c"
""
""
"e"
"f"
"" Split on " ," and elide delimiters
"a"
"b"
"c"
"e"
"f"
""

在C++中还有很多方法来实现split 函数,cplusplus.com有个C++ split 专题,详细比较分析了几种实现方法(见下图)。链接见文末参考文献。

#---------------------------------------------------------------------------------#

参考文献

《Accelerated C++》 by Andrew Koenig, Barbara E. Moo.

"Split a string" from cplusplus.com

C++常见问题: 字符串分割函数 split的更多相关文章

  1. (转)C++常见问题: 字符串分割函数 split

    http://www.cnblogs.com/dfcao/p/cpp-FAQ-split.html C++标准库里面没有字符分割函数split ,这可太不方便了,我已经遇到>3次如何对字符串快速 ...

  2. JavaScript中字符串分割函数split用法实例

    这篇文章主要介绍了JavaScript中字符串分割函数split用法,实例分析了javascript中split函数操作字符串的技巧,非常具有实用价值,需要的朋友可以参考下 本文实例讲述了JavaSc ...

  3. Delphi 自带的字符串分割函数split

    下面介绍Delphi自带的字符串分割函数,根据你的需要来使用. 1.ExtractStrings function ExtractStrings(Separators, WhiteSpace: TSy ...

  4. C++之字符串分割函数split

    c++之字符串分割: /* *c++之字符串分割: */ #include <iostream> #include <string> #include <vector&g ...

  5. Java字符串分割函数split源码分析

    spilt方法作用 以所有匹配regex的子串为分隔符,将input划分为多个子串. 例如: The input "boo:and:foo", for example, yield ...

  6. SQL Server自定义字符串分割函数——Split

    我相信大部分人都碰到过,处理数据的时候,字段的值是以 ',' (逗号)分隔的形式,所以我也不能避免. 然后我才知道,sql 是没有类似于 C# 和 Javascript 这种分割字符串的方法.( Sp ...

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

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

  8. Split字符串分割函数

    非常非常常用的一个函数Split字符串分割函数. Dim myTest myTest = "aaa/bbb/ccc/ddd/eee/fff/ggg" Dim arrTest arr ...

  9. ASP.NET中常用的字符串分割函数

    asp.net字符串分割函数用法 先来看个简单的实例 但是其数组长度却是25,而不是3.下面这种方法是先将“[111cn.net]”替换成一个特殊字符,比如$,在根据这个字符执行Split 例如下面我 ...

随机推荐

  1. ZOJ Problem Set - 1338 Up and Down Sequences 解释 ac代码

    这道题目我一开始一头雾水,怎么都数不对,参考了下网上的博文,才弄懂. 题意是这样的,如果是上升序列,上升序列的长度不是所有上升数字的,是这么规定的,如果它与前一个数字构成上升,那么这个数字算上长度.所 ...

  2. Struts2 源码分析——配置管理之ContainerProvider接口

    本章简言 上一章笔者讲到关于Dispatcher类的执行action功能,知道了关于执行action需要用到的信息.而本章将会讲到的内容也跟Dispatcher类有关系.那就是配置管理中的Contai ...

  3. 基于STM32Cube的DAC数模转化

    1. STM32Cube配置 1.1 DAC配置   1.2 TIM6 配置 1.3 利用Cube产生工程程序,MDK打开软件       在主循环上添加语句:      HAL_TIM_Base_S ...

  4. 安装DotNetCore.1.0.1-VS2015Tools.Preview2.0.2出现0x80072f8a未指定的错误

    本文转载自: http://www.cnblogs.com/JiaoWoWeiZai/p/5892255.html 最近DotNetCore更新到了1.0.1,Azure tools也更新到了2.9. ...

  5. 绿色简单的学校登录html页面

    效果预览:http://hovertree.com/texiao/css/22/ 代码如下: <!DOCTYPE html> <html> <head> <m ...

  6. jQuery css3鼠标悬停图片显示遮罩层动画特效

    jQuery css3鼠标悬停图片显示遮罩层动画特效 效果体验:http://hovertree.com/texiao/jquery/39/ 效果图: 源码下载:http://hovertree.co ...

  7. 【C#】可空类型(Nullable)

    C# 可空类型(Nullable) C# 提供了一个特殊的数据类型,nullable 类型(可空类型),可空类型可以表示其基础值类型正常范围内的值,再加上一个 null 值. 例如,Nullable& ...

  8. C++_系列自学课程_第_6_课_bitset集_《C++ Primer 第四版》

    在C语言中要对一个整数的某一个位进行操作需要用到很多的技巧.这种情况在C++里面通过标准库提供的一个抽象数据类型 bitset得到了改善. 一.标准库bitset类型 1.bitset的作用 bits ...

  9. struts2+spring的两种整合方式

    也许有些人会因为学习了struts1,会以为struts2.struts1与spring的整合也是一样的,其实这两者相差甚远.下面就来讲解一下struts2与spring的整合两种方案.(部分转载,里 ...

  10. Linux 定时任务crontab

    crontab定时任务格式 1 * * * * * command 2 第1列表示分钟1-59 每分钟用*或者 */1表示 3 第2列表示小时1-23(0表示0点) 4 第3列表示日期1-31 5 第 ...