在C++中实现字符串分割--split
字符串分割
在一些比较流行的语言中,字符串分割是一个比较重要的方法,不论是在python,java这样的系统级语言还是js这样的前端脚本都会在用到字符串的分割,然而在c++中却没有这样的方法用来调用。但是在boost中却提供分割方法。
使用vector实现
下面是用vector实现的一个简单的split函数,借助string::find函数查找匹配子串的位置,然后截取剩下的字符串之后继续处理,实现对原字符串的完整处理。
#include<iostream>
#include<vector>
#include<string>
using namespace std;
vector<string> split(string &s, string &delim, bool removeEmpty = false, bool fullMatch = false) {
vector<string> result;
string::size_type start = 0, skip = 1;
// 使用整个字符串进行查找,之后会直接跳过整个子字符串
if (fullMatch) {
skip = delim.length();
}
while (start != string::npos) {
// 从start位置开始查找子字符串delim第一次出现的位置
string::size_type finsh = s.find(delim, start);
if (skip == 0) {
finsh = string::npos;
}
// 从start开始到(finsh - start)处获得子字符串
// 获得匹配字符串之前的字符串
string token = s.substr(start, finsh - start);
// 对token进行判断并决定是否移除空
if (!(removeEmpty && token.empty())) {
// 将匹配字符串之前的字符串放进向量中
result.push_back(token);
}
// 判断此时是否到了原字符串的末尾
if ((start = finsh) != string::npos) {
// 将子字符串放进向量中,是为了保证原字符串的字符不丢失
result.push_back(delim);
// 将查找位置向前移动skip个位置
start = start + skip;
}
}
return result;
}
int main() {
string x = "hello,,world",
delim = ",";
vector<string> xx = split(x, delim, true, true);
for (auto item:xx) {
cout << item << endl;
}
cout << "finsh" << endl;
return 0;
}
在C++中实现字符串分割--split的更多相关文章
- JS对象 字符串分割 split() 方法将字符串分割为字符串数组,并返回此数组。 语法: stringObject.split(separator,limit)
字符串分割split() 知识讲解: split() 方法将字符串分割为字符串数组,并返回此数组. 语法: stringObject.split(separator,limit) 参数说明: 注意:如 ...
- Javascript:字符串分割split()妙用
概述: split() 方法将字符串分割为字符串数组,并返回此数组 语法格式: stringObject.split(separator,limit) 参数说明: 注意:如果把空字符串 (" ...
- Java中的字符串分割 .
转自 http://blog.csdn.net/yuwenhao0518/article/details/7161059 http://longkm.blog.163.com/blog/static/ ...
- java中的字符串分割函数
java中的split函数和js中的split函数不一样. Java中的我们可以利用split把字符串按照指定的分割符进行分割,然后返回字符串数组,下面是string.split的用法实例及注意事项: ...
- 字符串分割split()
知识讲解: split() 方法将字符串分割为字符串数组,并返回此数组. 语法: stringObject.split(separator,limit) 参数说明: 注意:如果把空字符串 (" ...
- lua脚本中字符串分割split
function split( s, c ) for item in string.gmatch( s, "(.-)"..c) do print(item); end end s ...
- C语言中的字符串分割函数
char *strtok(char *s, const char *delim); 分解字符串为一组字符串.s为要分解的字符串,delim为分隔符字符串. 从s开头开始的一个个被分割的串.当没有被分割 ...
- 34. Studio字符串分割split用法
var v = "1,2,3"; var arr = v.toString().split(","); 备注:最好先toString()转为字符串,不然有些情况 ...
- ruby中的字符串分隔符--split
当字符串是以“:”隔开时,可以这样写: column = str.split(/:/) 这样,column就是字符串每栏的值所构成的数组. eg: str = "Ruby in a shel ...
随机推荐
- Android Studio教程--给Android Studio安装Genymotion插件
打开Android Studio,依次[File]-[Settings] 在打开的settings界面里找到plugins设置项,点击右侧的“Browser..”按钮 在搜索栏里输入genymotio ...
- js confirm函数 删除提示
<a href="del.php" onclick="return confirm('是否将此留言信息删除?')">删除留言</a>
- JavaScript SetInterval与setTimeout使用方法详解
setTimeout和setInterval的语法相同.它们都有两个参数,一个是将要执行的代码字符串,还有一个是以毫秒为单位的时间间隔,当过了那个时间段之后就将执行那段代码.不过这两个函数还是有区别的 ...
- MVC数据库数据分页显示
首先从数据库获取数据 using System; using System.Collections.Generic; using System.Linq; using System.Web; usin ...
- IIS和tomcat共用80端口
IIS和tomcat共用80端口 很多机器都需要同时使用tomcat和iis两个服务器以部署不同的网站,而解决共用80端口的问题也经常遇到,今天实际操作了一回,以下是具体步骤: 实现tomcat和ii ...
- 这个错误,每个ScrumMaster都犯过
[小编]ScrumMaster要授之以渔,还是授之以鱼?从04年开始接触XP,到08年自己的团队开始提出敏捷的概念,再到10年接受ScrumMaster培训:在刚开始做ScrumMaster的一段时间 ...
- 正则表达式 exec 获取字符串中的汉字
要求:仅获取attr中的 “编辑发起状态的执行人表单” ,路径C:\fakepath\是不固定的,可以是C:\fakepath\hhh\hhhh\ 解决: var attr = C:\fakepath ...
- yii2解决百度编辑器umeditor图片上传问题
作者:白狼 出处:http://www.manks.top/article/yii2_umeditor_upload本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原 ...
- First glance in Go
Because I forgot to install the Chinese input in this OS, I have to use English input. The first pro ...
- linux的“自动化”
h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h ...