c++ string中的replace

将原string 中的元素或子串替换。返回替换后的string。

(1)用string 或C-string 代替操作string 中从 _Pos1 开始的 _Num1 个字符

basic _ string& replace( size _ type _Pos1 ,size _ type _Num1 , const value _ type* _Ptr );

basic _ string& replace(size _ type _Pos1 ,size _ type _Num1 ,const basic _ string _Str );

c++ string 的函数replace()用法  - h-zy2008 - h-zy2008的博客
stringa,b;

strings("AAAAAAAA");

strings1p("BBB");

constchar*cs1p="CCC";

a
=s.replace(1,3,s1p);//s="ABBBAAAA"

b
=s.replace(5,3,cs1p);//


(2)用string 中从 _Pos2 开始的 _Num2 个字符,代替操作string 中从 _Pos1 开始的 _Num1 个字符

用C-string 中的 _Num2 个字符,代替操作string 中从 _Pos1 开始的 _Num1 个字符

basic _ string& replace( size _ type _Pos1 , size _ type _Num1 , const basic _ string& _Str ,

size _ type _Pos2 , size _ type );

basic _ string& replace( size _ type _Pos1 , size _ type _Num1 ,

const value _ type* _Ptr , size _ type _Num2 );

c++ string 的函数replace()用法  - h-zy2008 - h-zy2008的博客
stringa,b;

strings("AAAAAAAA");

strings2p("BBB");

constchar*cs2p="CCC";

a
=s.replace(1,3,s2p,1,2);//s="ABBAAAA"

b
=s.replace(4,3,cs2p,1);//


(3)用 _Count 个character _Ch , 代替操作string 中从 _Pos1 开始的 _Num1 个字符

basic _ string& replace( size _ type _Pos1 , size _ type _Num1 ,

size _ type _Count , value _ type _Ch );

string result;

string s( " AAAAAAAA " );

char ch = ' C ' ;

result
= s.replace( 1 , 3 , 4 ,ch); // s="ACCCCAAAA"

(4)用string 或C-string ,代替操作string 中从 First0 到 Last0 的字符

basic _ string&replace(iterator First0 ,iterator Last0 , const basic _ string& _Str );

basic _ string&replace(iterator First0 ,iterator _Last0 , const value _ type* _Ptr );

c++ string 的函数replace()用法  - h-zy2008 - h-zy2008的博客
strings("AAAAAAAA");strings4p("BBB");

constchar*cs4p="CCC";

basic_string
<char>::iteratorIterF0,IterL0;

IterF0
=s.begin();IterL0=s.begin()+3;

stringa,b;

a
=s.replace(IterF0,IterL0,s4p);//s="BBBAAAAA"

b
=s.replace(IterF0,IterL0,cs4p);//


(5)用string 中从 _Pos2 开始的 _Num2 个字符,代替操作string 中从 First0 到 Last0 的字符

用C-string 中的 _Num2 个字符,代替操作string 中从 First0 到 Last0 的字符

basic _ string& replace( iterator _First0 , iterator _Last0 ,

const value _ type* _Ptr , size _ type _Num2 );

template<class InputIterator> basic _ string& replace(

iterator _First0 , iterator _Last0 ,

InputIterator _First , InputIterator _Last );

IterF3 = s.begin ( ) + 1; IterL3 = s.begin ( ) + 3;

IterF4 = s.begin ( ); IterL4 = s.begin ( ) + 2;

a = s.replace ( IterF3 , IterL3 , IterF4 , IterL4 );

b = s.replace ( IterF1 , IterL1 , cs5p , 4 );

(6)用 _Count 个character _Ch , 代替操作string 中从 First0 到 Last0 的字符

basic _ string& replace( iterator _First0 , iterator _Last0 ,

size _ type _Count , value _ type _Ch );

a = s.replace ( IterF2 , IterL2 , 4 , ch );

会经常用到的函数:

// Test1.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <iostream> #include <string> using namespace std; string& replace_all(string& str,const string& old_value,const string& new_value) { while(true) { string::size_type pos(0); if( (pos=str.find(old_value))!=string::npos ) str.replace(pos,old_value.length(),new_value); else break; } return str; } string& replace_all_distinct(string& str,const string& old_value,const string& new_value) { for(string::size_type pos(0); pos!=string::npos; pos+=new_value.length()) { if( (pos=str.find(old_value,pos))!=string::npos ) str.replace(pos,old_value.length(),new_value); else break; } return str; } int main() { //cout<<str; string str = "lihan"; str.replace(str.begin(),str.end()-1,2,'s'); cout<<str; cout << replace_all(string("12212"),"2","3") << endl; cout << replace_all_distinct(string("12212"),"12","21") << endl; }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值