在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 ...
随机推荐
- Wifite.py 修正版脚本代码
Kali2.0系统自带的WiFite脚本代码中有几行错误,以下是修正后的代码: #!/usr/bin/python # -*- coding: utf-8 -*- """ ...
- [Android]使用AdapterTypeRender对不同类型的item数据到UI的渲染
以下内容为原创,转载请注明: 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/3992843.html 本文讲的工具均放在AndroidBucket开源 ...
- 图形学理论知识 BRDF 双向反射分布函数(Bidirectional Reflectance Distribution Function)
图形学理论知识 BRDF 双向反射分布函数 Bidirectional Reflectance Distribution Function BRDF理论 BRDF表示的是双向反射分布函数(Bidire ...
- android加固系列—4.加固前先学会破解,无源码调试apk
[版权所有,转载请注明出处.出处:http://www.cnblogs.com/joey-hua/p/5138585.html] 项目关键java代码为,将tv设置为从jni读取的字符串,这里的破解内 ...
- 【代码笔记】iOS-短信分享
代码: ViewController.h #import <UIKit/UIKit.h> //短信分享 #import <MessageUI/MFMailComposeViewCon ...
- location of the android sdk has not been setup in the preferences
点eclipse的android virtual device manager提示错误:error:location of the android sdk has not been setup in ...
- Log4jConfigListener cannot be found — context fails to start
解决方案 Reticulogic's second suggestion is correct. However, in Eclipse Helios, the "Java EE Modul ...
- Java 控制线程
1.join public class JoinThreadTest extends Thread { public JoinThreadTest(String name){ super(name); ...
- 安装php扩展
下面我以soap安装为例子 cd /home/zhangy/php-5.2.6/ext/soap /usr/local/php/bin/phpize#确定php-config文件在不在,调用php-c ...
- BIEE使用技巧
索引: 1.如何清除缓存 2.通过“编辑 SQL”取得前一天的日期 3.格式化日历框参数 4.根据传入的开始时间和结束时间取得事实表中的指标(用到了3中的技巧) 5.直接调用数据库函数 6.时间格式转 ...