(1)C++中字符串的替换/截取/分割 (笔记)

(1)C++字符串的一些用法记录:

替换函数:regex_replace()

截取字符串的函数:substr()

自动分割函数:Split()


1、替换函数 replace() 和 regex_replace()

c++中的replace函数无法替换指定的字符串
//这里的参数2也可以为const string& str,意思是一样的,都是字符串
string& replace(int pos,int n,const char* s) //替换从pos开始的n个字符为字符串s
如果要替换指定的字符串:
方法1:需要结合find()函数:
首先介绍一下find函数和rfind函数:

find()函数:作用为从左往右,查找目标字符或字符串。查找成功则返回第一个目标字符或者字串的位置。

//find函数原形:查找str第一次出现位置,从pos开始查找,完成后返回int类型的下标值,这里的参数1也可以为const char* c,意思一样的
int find(const string& str, int pos = 0)const
//实例
string num = "I love china";
int pos = num.find("lo");
cout << pos << endl;//输出2

这里还有一个rfind()函数,其是从右往左,查找第一个目标字符的位置

//rfind函数原型:查找str第一次出现的位置,从pos开始查找,并返回int类型的下标 ,这里的参数1也可以为const char* c,意思一样的
int rfind(const string& str, int pos = npos)const
//示例
string num = "I love china lo";
int pos = num.rfind("lo");
cout << pos << endl;//输出13

可以看到find和rfind的区别就是,find是从左往右找第一个目标字符。rfind是从右往左,也就是找最后一个目标字符。


接下来利用replace结合find函数, 希望能够查找出所有指定的目标字符串并进行替换,行不行呢?请看下面的示例:
#include <iostream>
#include <string>
using namespace std;

int main()
{
	string str("123454321");
	string a, b;
	a = str.replace(str.find("2"), 1, "*");
	cout << a << endl; //输出 1*3454321
	return 0;
}

可以看到,就算结合了find()函数,也只能返回第一个查找成功的字符或字符串的位置。因此,如果希望替换掉字符串中所有指定的字符或字符串,可以用regex_replace()。具体用法如下:

先看下列代码:

//头文件
#include <iostream>
#include <string>
#include <regex>
using std::cout; using std::cin;
using std::endl; using std::string;
//利用regex_replace必须具备以下两个
using std::regex_replace; using std::regex;

int main() 
{
	string equation = "x+6-x+3=6-x-3";//定义一个字符串
	string order = "+-";//定义目标字符串(也可以是字符)

	//Step1: 利用正则替换 regex_replace 将字符串中的"-"号 替换 为"+-"号
	string equ = regex_replace(equation, regex("-"), order);
	
	cout << equ <<endl; //输出: x+6+-x+3=6+-x+-3
	}
	return 0;
}

因此我们可以直到正则替换函数中的参数的意义为:

regex_replace(std::string str,std::regex reg,std::string replace)

参数1:std::string: 返回值:为原字符串string执行 替换规则后 输出的新的字符串str
参数2:std::regex reg:替换规则中 被替换 的 原目标字符或字符串
参数3:std::string replace 替换规则中 替换 的 新目标字符或字符串

正则替换的多种用法链接


2、截取子字符串的函数:substr()

先看下面代码:

	//Step2:利用substr截取一段字符串
	string equ = "x+6+-x+3=6+-x+-3"
	for (int i = 0; i < equ.length(); i++)
	{
		if (equ[i] == '=')
		{
			string left_equ = equ.substr(0, i); //截取"="号左边字符串
			string right_equ = equ.substr(i + 1);//截取"="号右边字符串
			cout << "=号左边字符串: " << left_equ << endl;//输出: =号左边字符串: x+6+-x+3
			cout << "=号右边字符串:" << right_equ << endl;//输出:=号右边字符串: 6+-x+-3
		}
	}

其中:

substr用法1:
str.substr(pos,len)
参数1  pos:字符串起始的截取位置下标
参数2  len:截取字符串的个数

substr用法2:
str.substr(pos)
pos的默认值是0,也就是说默认从字符串的起始位置开始截取子字符串
len的默认值是str.size()-pos,也就是说如果没有参数len,会默认截取从pos开始的整个子字符串str

3、分割函数split()

C++的标准库中没有字符分割函数split(),因此只能使用其函数原型strtok(),具体使用看如下代码:

#define _CRT_SECURE_NO_DEPRECATE  //不加该定义的化会报错(vs2017中,其他版本不清楚)
#include <iostream>
#include <string>
using namespace std;

int main()
{
	string equation = "x+6-x+3=6-x-3";
	const char *equ = equation.data();//string类型转化为char类型
	const char *d = "+";
	char *p;
	p = strtok((char *)equ, d);
	while (p != NULL)
	{
		cout << p<<endl;
		p = strtok(NULL, d);
	}
	system("pause");
	return 0;
}

其中

char *strtok(char *str,const char *delim);
参数1:需要分割的原字符串
参数2:分割的目标字符或字符串
  • 10
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

徽州SLAM李

如果觉得不错,打赏一下哦,嘻

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值