c++ string中的replace

来源:互联网 发布:lte网络架构图 编辑:程序博客网 时间:2024/06/05 15:54

将原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 );


 

(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 );


 

(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 );


 

(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;   }   





原创粉丝点击