C++常用字符串分割方法(转)
1、用strtok函数进行字符串分割
原型: char *strtok(char *str, const char *delim);
功能:分解字符串为一组字符串。
参数说明:str为要分解的字符串,delim为分隔符字符串。
返回值:从str开头开始的一个个被分割的串。当没有被分割的串时则返回NULL。
其它:strtok函数线程不安全,可以使用strtok_r替代。
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
//借助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); } return 0; } |
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=0; i<result.size(); i++)
{
std::cout<<result[i]<<std::endl;
}
std::cin.get();
std::cin.get();
return
0;
}
3、find_first_not_of
#include<string>
#include<vector>
#include<iostream>
using namespace std; void Tokenize(const string& str, vector<string>& tokens, const string& delimiters)
{
// Skip delimiters at beginning.
string::size_type lastPos = str.find_first_not_of(delimiters, );
// Find first "non-delimiter".
string::size_type pos = str.find_first_of(delimiters, lastPos);
while (string::npos != pos || string::npos != lastPos)
{
// Found a token, add it to the vector.
tokens.push_back(str.substr(lastPos, pos - lastPos));
// Skip delimiters. Note the "not_of"
lastPos = str.find_first_not_of(delimiters, pos);
// Find next "non-delimiter"
pos = str.find_first_of(delimiters, lastPos);
}
}
int main(int argc, char *argv[])
{
string str("====aaa==bbb=ccc=ddd====");
vector<string>tokens;
Tokenize(str, tokens, "=");
for( int i = ; i < tokens.size() ; i++ )
{
cout << tokens[i] << endl;
}
return ;
}
4、用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,-1);
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=0,size=vec.size();i<size;i++)
{
std::cout<<vec[i]<<std::endl;
}
std::cin.get();
std::cin.get();
return
0;
}
5、最近发现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 ;
}
C++常用字符串分割方法(转)的更多相关文章
- [转]C++常用字符串分割方法实例汇总
本文实例汇总了C++常用字符串分割方法,分享给大家供大家参考.具体分析如下: 我们在编程的时候经常会碰到字符串分割的问题,这里总结下,也方便我们以后查询使用. 一.用strtok函数进行字符串分割 原 ...
- C++常用字符串分割方法实例汇总
投稿:shichen2014 字体:[增加 减小] 类型:转载 时间:2014-10-08我要评论 这篇文章主要介绍了C++常用字符串分割方法实例汇总,包括了strtok函数.STL.Boost等常用 ...
- C++常用字符串分割方法
一.用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 ...
随机推荐
- Super-Resolution Restoration of MISR Images Using the UCL MAGiGAN System 超分辨率恢复
作者是伦敦大学学院Mullard空间科学实验室成像组,之前做过对火星图像的分辨率增强. 文章用了许多的图像处理方法获得特征和高分辨率的中间结果,最后用一个生产对抗网络获得更好的高分辨率结果. 用的数据 ...
- python解释器安装
1. 下载安装包 1 2 https://www.python.org/ftp/python/2.7.14/python-2.7.14.amd64.msi # 2.7安装包 https: ...
- for别名
name:for(int i =0 ;i< a.length;i++){ System.out.println(i); for(int j =0;j<i;j++){ continue na ...
- C# DateTime.Compare时间大小对比
- C#面试题-递归
有一组数1.1.2.3.5.8.13.21.34...,求第n个数是多少? public int Recursion (int n){ ){ ; } &&n<=){ ; } )+ ...
- python--selectors模块
它的功能与linux的epoll,还是select模块,poll等类似:实现高效的I/O multiplexing, 常用于非阻塞的socket的编程中: 简单介绍一下这个模块,更多内容查看 pyt ...
- 创建本地Git并提交到码云
概述 安装Git,使用Git Bash创建本地Git全局用户名,提交远程代码时将以此用户名显示git config --global --replace-all user.email "it ...
- mpdf Could not find image file (http://local.com/xxxxx)
记录一下昨天和今天遇到的,yii2使用mpdf的时候,图片是使用php方法生成的二维码,所以图片地址为http://local.com/xxxxx,url中携带不同的参数. 但是开启了 $mpdf-& ...
- HAL库延时、SYCCNT与SYSTICK
HAL库驱动中,由于某些外设的驱动需要使用超时判断(比如I2C.SPI.SDIO等),需要精确延时(精度为1ms),使用的是SysTick,但是在操作系统里面,我们需要使用SysTick来提供系统时基 ...
- select获取到option的value和text方法
function getSelectval(id){ var selId = document.getElementById(id); //获取select的id var seleIndex =sel ...