在C++中没有直接对应的split函数,字符串分割可借助以下方法实现:

1、借助strtok函数

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

函数功能:以delim为分隔符分割字符串str

参数说明:str:要分隔的字符串;delim:分隔符

返回值:从str开头开始的一个个被分割的字符串。当没有被分割时则返回null

代码1:直接使用strtok函数分割char*类型的字符串

代码2:借助strtok分割string类型的字符串,将结果保存在vector<string>中

#include <iostream>
using namespace std; int main() {
char s[] = "my name is lmm";
char *p;
const char *delim = " ";
p = strtok(s, delim);
while(p) {
cout << p << endl;
p = strtok(NULL, delim);
} return 0;
}

  

思路:先将整个string字符串转换为char*类型,分割后得到char*类型的子字符串,将子字符串转换为string类型,并存入结果数组中。

#include <iostream>
#include <vector>
using namespace std; vector<string> split(const string& str, const string& delim) {
vector<string> res;
if("" == str) return res;
//先将要切割的字符串从string类型转换为char*类型
char * strs = new char[str.length() + 1] ; //不要忘了
strcpy(strs, str.c_str()); char * d = new char[delim.length() + 1];
strcpy(d, delim.c_str()); char *p = strtok(strs, d);
while(p) {
string s = p; //分割得到的字符串转换为string类型
res.push_back(s); //存入结果数组
p = strtok(NULL, d);
} return res;
} void test1() { //空字符串
cout << "******test1****** "<<endl;
string s = ""; std::vector<string> res = split(s, " ");
for (int i = 0; i < res.size(); ++i)
{
cout << res[i] <<endl;
}
} void test2() { //只有一个字符串
cout << "******test2****** " <<endl;
string s = "my"; std::vector<string> res = split(s, " ");
for (int i = 0; i < res.size(); ++i)
{
cout << res[i] <<endl;
}
} void test3() { //正常字符串
cout << "******test3****** "<<endl;
string s = "my name is lmm ";//连续多个空格,空格会被过滤掉 std::vector<string> res = split(s, " ");
for (int i = 0; i < res.size(); ++i)
{
cout << res[i] <<endl;
}
} int main() { test1();
test2();
test3();
return 0;
}

  

注意:test3中连续多个空格出现,空格都会被过滤掉

2、借助于string类的find和substr函数

1)find函数

函数原型:size_t find(const string& str, size_t pos = 0) const;

功能说明:从pos位置开始查找子字符串str第一次出现的位置

参数说明:str为要查找的子字符串,pos从为初始查找位置

返回值:找到的话返回子字符串第一次出现的位置,否则返回string::npos

2)substr函数

函数原型:string substr(size_t pos = 0, size_t n = npos) const;

功能说明:获取从指定的起始位置开始,长度为n的子字符串

参数说明:pos为起始位置,n获取的1字符串长度

返回值:子字符串

#include <iostream>
#include <string>
#include <cstring>
#include <vector>
using namespace std; string reverse_one_word(string str) {
for(int i = 0; i < str.length()/2; i ++) {
char tmp;
tmp = str[i];
str[i] = str[ str.length() - i - 1 ];
str[ str.length() - i - 1 ] = tmp;
}
return str;
} vector<string> split(const string& str,const string& delim) { //将分割后的子字符串存储在vector中
vector<string> res;
if("" == str) return res; string strs = str + delim; //*****扩展字符串以方便检索最后一个分隔出的字符串
size_t pos;
size_t size = strs.size(); for (int i = 0; i < size; ++i) {
pos = strs.find(delim, i); //pos为分隔符第一次出现的位置,从i到pos之前的字符串是分隔出来的字符串
if( pos < size) { //如果查找到,如果没有查找到分隔符,pos为string::npos
string s = strs.substr(i, pos - i);//*****从i开始长度为pos-i的子字符串
res.push_back(s);//两个连续空格之间切割出的字符串为空字符串,这里没有判断s是否为空,所以最后的结果中有空字符的输出,
i = pos + delim.size() - 1;
} }
return res;
} void test1() { //空字符串
cout << "******test1****** "<<endl;
string s = ""; std::vector<string> res = split(s, " ");
for (int i = 0; i < res.size(); ++i)
{
cout << res[i] <<endl;
}
} void test2() { //只有一个字符串
cout << "******test2****** " <<endl;
string s = "my"; std::vector<string> res = split(s, " ");
for (int i = 0; i < res.size(); ++i)
{
cout << res[i] <<endl;
}
} void test3() { //正常字符串
cout << "******test3****** "<<endl;
string s = "my name is lmm "; std::vector<string> res = split(s, " ");
for (int i = 0; i < res.size(); ++i)
{
cout << res[i] <<endl;
}
} int main() { test1();
test2();
test3();
return 0;
}

  

注意:test3中的多个空格未被过滤掉,也就是说两个空格分隔符之间的空子串也被存进了结果数组中。要想避免这个问题可以在分隔出子字符串s时,判断一下若为空(两个分隔符相邻,中间的子串为空),则不加入字符数组即可去掉。

C++之split字符串分割的更多相关文章

  1. Split字符串分割函数

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

  2. freemarker中的split字符串分割

    freemarker中的split字符串分割 1.简易说明 split分割:用来根据另外一个字符串的出现将原字符串分割成字符串序列 2.举例说明 <#--freemarker中的split字符串 ...

  3. freemarker中的split字符串分割(十六)

    1.简易说明 split分割:用来根据另外一个字符串的出现将原字符串分割成字符串序列 2.举例说明 <#--freemarker中的split字符串分割--> <#list &quo ...

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

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

  5. javascript中split字符串分割函数

    1. var ss=s.split("fs"); for(var i=0;i<ss.length;i++){ 处理每一个ss[i]; } 2. "2:3:4:5&q ...

  6. 【pandas】pandas.Series.str.split()---字符串分割

    原创博文,转载请注明出处! 本文代码的github地址       series中的元素均为字符串时,通过str.split可将字符串按指定的分隔符拆分成若干列的形式. 例子: 拆分以逗号为分隔符的字 ...

  7. freemarker split字符串分割 遍历map

    <#list "张三三,李思思,,王强,柳树,诸葛正我"?split(",") as name> "${name}" </ ...

  8. java/Android String.split 字符串分割

    特殊符号分割时需加[].如下图

  9. 【转载】 C++之split字符串分割

    https://blog.csdn.net/mary19920410/article/details/77372828

  10. 在C++中实现字符串分割--split

    字符串分割 在一些比较流行的语言中,字符串分割是一个比较重要的方法,不论是在python,java这样的系统级语言还是js这样的前端脚本都会在用到字符串的分割,然而在c++中却没有这样的方法用来调用. ...

随机推荐

  1. 自定义StringByteLength

    using HKElectric.ESafety.Utilities; using System.ComponentModel.DataAnnotations; namespace HKElectri ...

  2. ubuntu20.04安装fastdfs遇到的问题

    说明:git clone在线安装与离线安装都不成功后,选择原来可以正常运行的fastdfs服务,进行tar打包下载,再在新项目上进行解压部署.但由于打包压缩动态库的软连接 失效,所以启动出现如下报错信 ...

  3. 如何用HP 39GS计算器画出双曲线图像

    1.双曲线标准方程和参数方程 2.计算器上的操作 1.打开APLET->Parametric->START 2.设置X1(T)=3/COS(T),X2(T)=4*TAN(T) 3.SHIF ...

  4. 微信内h5调用支付

    在做公众号商城的时候,需要用到调用微信支付,这是微信官方文档教程 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_7&in ...

  5. 苹果App 上架 app store 提示 “一张或多张屏幕快照的尺寸存在错误“

    获取预览图的方式有两种:第一种是用真机运行然后截图获取:另一种是用模拟器运行然后截图获取. 先说说第一种获取方式:app运行在5.5英寸(6+)的手机上,截取屏幕快照.在上传时会报错:"一张 ...

  6. software_programming

    2020-04-04  12:05:43 todo list Java8 实战2> chapter2 行为参数化 业务逻辑的隔离 DSL

  7. golang json 字符串 用 json.Number 解析字段

    不定义结构体,用map 解析json 字符串字段 func main() { jsonString := `{"age": 20, "height": 180 ...

  8. “const char*“类型的实参与“LPCWSTR-类型的形参不兼容

    const char *类型的实参与LPCWSTR类型的形参不兼容解决 LPCWSTR类型是是一个指向unicode编码字符串的32位指针,所指向字符串是wchar型(4字节,Unicode编码宽字节 ...

  9. 统一单点登录&跳转

    在客户端输入地址(xxx.xxx.xxx/controll/方法/参数) 服务端到controll层 进行数据匹配 跳转 前端响应情况 图片跳转与列表跳转 图片:编写跳转函数 列表公用图片跳转函数,@ ...

  10. 18.SQLite应用案例-课程表

    一.程序界面 1.课程表首页 一周有7天,一天有10节课. 课程表首页的布局activity_main.xml框架设计大致如此: 最外层使用线性布局设置屏幕水平方向android:orientatio ...