C++常用字符串分割方法
一、用strtok函数进行字符串分割
原型: char *strtok(char *str, const char *delim);
功能:分解字符串为一组字符串。
参数说明:str为要分解的字符串,delim为分隔符字符串。
返回值:从str开头开始的一个个被分割的串。当没有被分割的串时则返回NULL。
其它:strtok函数线程不安全,可以使用strtok_r替代。
strtok内部记录上次调用字符串的位置,所以不支持多线程,可重入版本为strtok_r,有兴趣的可以研究一下。它适用于分割关键字在字符串之间是“单独”或是 “连续“在一起的情况。
注意:因为函数内部会修改原字符串变量,所以传入的参数不能是不可变字符串(即不能是字符串常量区)。
如 char *tokenremain ="abcdefghij"//编译时为文字常量,不可修改。
strtok(tokenremain,"cde");
strsep(&tokenremain,"cde");
编译通过,运行时会报段错误。
这种情况必须先把字符串赋值为可修改的变量。
//借助strtok实现split
#include <string.h>
#include <stdio.h> 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); //注意这里,strtok内部记录了上次的位置
} return ;
}
还有strsep函数,自己百度一下例子。[http://www.cnblogs.com/longzhao1234/archive/2012/05/31/2528317.html]
strsep返回值为分割后的开始字符串,并将函数的第一个参数指针指向分割后的剩余字符串。它适用于分割关键字在两个字符串之间只严格出现一次的情况。
二、用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)
返回值:子字符串
#include <iostream>
#include <string>
#include <vector> //字符串分割函数
std::vector<std::string> split(std::string str,std::string pattern)
{
std::string::size_type pos;
std::vector<std::string> result;
str+=pattern;//扩展字符串以方便操作
int size=str.size(); for(int i=; i<size; i++)
{
pos=str.find(pattern,i);
if(pos<size)
{
std::string s=str.substr(i,pos-i);
result.push_back(s);
i=pos+pattern.size()-;
}
}
return result;
} int main()
{
std::string str;
std::cout<<"Please input str:"<<std::endl;
//std::cin>>str;
getline(std::cin,str);
std::string pattern;
std::cout<<"Please input pattern:"<<std::endl;
//std::cin>>pattern;
getline(std::cin,pattern);//用于获取含空格的字符串
std::vector<std::string> result=split(str,pattern);
std::cout<<"The result:"<<std::endl;
for(int i=; i<result.size(); i++)
{
std::cout<<result[i]<<std::endl;
} std::cin.get();
std::cin.get();
return ;
}
三、用Boost进行字符串的分割
用boost库的正则表达式实现字符串分割
#include <iostream>
#include <cassert>
#include <vector>
#include <string>
#include "boost/regex.hpp" std::vector<std::string> split(std::string str,std::string s)
{
boost::regex reg(s.c_str());
std::vector<std::string> vec;
boost::sregex_token_iterator it(str.begin(),str.end(),reg,-);
boost::sregex_token_iterator end;
while(it!=end)
{
vec.push_back(*it++);
}
return vec;
}
int main()
{
std::string str,s;
str="sss/ddd/ggg/hh";
s="/";
std::vector<std::string> vec=split(str,s);
for(int i=,size=vec.size();i<size;i++)
{
std::cout<<vec[i]<<std::endl;
}
std::cin.get();
std::cin.get();
return ;
}
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 ;
}
使用Boost库
1)使用boost::split。根据字符来split,注意is_any_of的意思是集合中的任何一个,而不是equals的意思,那如何根据字符串split原字符串?请看2和3。
#include <iostream>
#include <vector>
#include <string>
#include <boost/algorithm/string.hpp>
using namespace std;
int main()
{
string str( "dfa&fda|dfdf");
vector<string> tokens;
boost::split(tokens, str, boost::is_any_of("&|"));
for (size_t i = ; i < tokens.size(); ++ i)
{
cout << tokens[i] << endl;
}
return ;
}
2) 使用boost::tokenize,注意 boost::char_separator<char>分隔符一样是集合的概念!但划分方法和boost::split不大一样,详看boost文档。
#include <iostream>
#include <vector>
#include <string>
#include <boost/tokenizer.hpp>
using namespace std;
int main()
{
string str("dfa||fda||dfdf");
vector<string> tokens;
boost::char_separator<char> sep("|");
boost::tokenizer<boost::char_separator<char> > tok(str, sep);
tokens.clear();
std::copy(tok.begin(), tok.end(), std::back_inserter(tokens));
for (size_t i = ; i < tokens.size(); ++ i)
{
cout << tokens[i] << endl;
}
return ;
}
3)使用boost::regex
#include <iostream>
#include <vector>
#include <string>
#include <boost/regex.hpp>
using namespace std;
int main()
{
string str("dfa||fda||dfdf");
vector<string> tokens;
boost::regex reg("\\|\\|");
boost::sregex_token_iterator it(str.begin(), str.end(), reg, -);
boost::sregex_token_iterator end;
while (it!=end)
{
tokens.push_back(*it++);
}
for (size_t i = ; i < tokens.size(); ++ i)
{
cout << tokens[i] << endl;
}
return ;
}
C++常用字符串分割方法的更多相关文章
- [转]C++常用字符串分割方法实例汇总
本文实例汇总了C++常用字符串分割方法,分享给大家供大家参考.具体分析如下: 我们在编程的时候经常会碰到字符串分割的问题,这里总结下,也方便我们以后查询使用. 一.用strtok函数进行字符串分割 原 ...
- C++常用字符串分割方法实例汇总
投稿:shichen2014 字体:[增加 减小] 类型:转载 时间:2014-10-08我要评论 这篇文章主要介绍了C++常用字符串分割方法实例汇总,包括了strtok函数.STL.Boost等常用 ...
- C++常用字符串分割方法(转)
1.用strtok函数进行字符串分割 原型: char *strtok(char *str, const char *delim); 功能:分解字符串为一组字符串. 参数说明:str为要分解的字符串, ...
- JavaScript字符串分割方法
使用split('')方法.此方法与Java的字符串分割方法方法名一样.
- (数据科学学习手札131)pandas中的常用字符串处理方法总结
本文示例代码及文件已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 在日常开展数据分析的过程中,我们经常需要对 ...
- 【2019-5-26】python:字典、常用字符串处理方法及文件操作
一.数据类型:字典 1.字典: 1.1定义字典:dict={'key':'value'} 1.2字典与列表相比,字典取值快,可直接找到key 1.3字典是无序的,不能根据顺序取值 1.4多个元素用逗号 ...
- JS常用字符串处理方法应用总结
这篇文章主要总结了JS常用字符串的处理方法,需要的朋友可以参考下 1.indexOf()方法,从前往后查找字符串位置,大小写敏感,从0开始计数.同理,lastIndexOf() 方法从后往前,两个 ...
- 标准C中字符串分割方法
◆ 使用strtok函数分割. 原型:char *strtok(char *s, char *delim); strtok在s中查找包含在delim中的字符并用NULL('\0')来替换,直到找遍整个 ...
- js常用字符串处理方法
JS自带函数concat将两个或多个字符的文本组合起来,返回一个新的字符串.var a = "hello";var b = ",world";var c = a ...
随机推荐
- Linux菜鸟起飞之路【二】Linux基本常识
一.Unix操作系统基本常识 1.什么是Unix? Unix是一个计算机操作系统,是一个用来协调.管理和控制计算机硬件与软件资源的控制程序. 2.Unix操作系统的特点? 多用户与多任务.多用户表示在 ...
- svn设置提交时忽略某些文件
一.在资源管理器中,右键一个未加入版本控制文件或目录,并从弹出菜单选择TortoiseSVN →Add to Ignore List,会出现一个子菜单,允许你仅选择该文件或者所有具有相同后缀的文件. ...
- The US in understimating Huawei, says founder Ren zhengfei
Huawei Founder Ren Zhengfei has downplayed the impact of the US executive order that cripple Huawei' ...
- LeetCode 637. Average of Levels in Binary Tree(层序遍历)
Given a non-empty binary tree, return the average value of the nodes on each level in the form of an ...
- POJ:2406-Power Strings(寻找字符串循环节)
Power Strings Time Limit: 3000MS Memory Limit: 65536K Description Given two strings a and b we defin ...
- Linux下安装MySQLdb模块(Python)
一.MySQLdb-python模块 https://pypi.python.org/pypi/MySQL-python ` 二.安装依赖包 yum -y install python-devel m ...
- Java设计模式学习二
Java设计思想之单例模式 单例模式(Singleton Pattern)是Java中最常见的设计模式之一.这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式. 这种模式涉及到一个单一的 ...
- LongListSelector 控件 在 wp7 和wp8中的不同之处
众所周知,wp8中的LongListSelector集成到了Rom中. 性能得到了提升,一些api也发生了变化. 在这里总结一下,作为分享,也作为备忘. 参考文献 Windows Phone 8 XA ...
- x86实模式到保护模式 李忠 王晓波
x86实模式到保护模式 李忠 王晓波 第3到4章 各个进制间的转换省略 实验环境 编译器 nasm 虚拟机 virtual box 小程序 hexview 观察编译后的机器代码 fixvhd ...
- nginx进程属主问题讨论
文章作者:luxianghao 文章来源:http://www.cnblogs.com/luxianghao/p/6107096.html 转载请注明,谢谢合作. 免责声明:文章内容仅代表个人观点,如 ...