---
title: "c++ string中的replace"
author: "winzenghua"
date: "2009-10-27T11:28:00Z"
tags: []
slug: "c-string-replace"
draft: false
lastmod: "2009-10-27T11:28:00Z"
id: 947760
url: "https://java4gis.iteye.com/blog/947760"
---

将原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的博客](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif "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的博客](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif "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 );

stringresult;

strings("AAAAAAAA");

charch='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的博客](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif "c++ string 的函数replace()用法  - h-zy2008 - h-zy2008的博客")  
strings("AAAAAAAA");strings4p("BBB");

constchar\*cs4p="CCC";

basic\_string&lt;char&gt;::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&lt;class InputIterator&gt; 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 );

会经常用到的函数：