当前位置:   article > 正文

C++实现字符串替换的两种方法_c语言中的\t在c++中可用什么替换

c语言中的\t在c++中可用什么替换
#include<string>
#include<iostream>
using namespace std;

//第一种替换字符串的方法用replace()

void string_replace(string&s1,const string&s2,const string&s3)
{
	string::size_type pos=0;
	string::size_type a=s2.size();
	string::size_type b=s3.size();
	while((pos=s1.find(s2,pos))!=string::npos)
	{
		s1.replace(pos,a,s3);
		pos+=b;
	}
}

//第二种替换字符串的方法用erase()和insert()

void string_replace_2(string&s1,const string&s2,const string&s3)
{
	string::size_type pos=0;
	string::size_type a=s2.size();
	string::size_type b=s3.size();
	while((pos=s1.find(s2,pos))!=string::npos)
	{
		s1.erase(pos,a);
		s1.insert(pos,s3);
		pos+=b;
	}

}

【转自:http://blog.csdn.net/crazyer2010/article/details/9063847】

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/465698
推荐阅读
相关标签
  

闽ICP备14008679号