//用法一:用str替换字符串,从起始位置pos开始长度为len的字符
#include <iostream>
#include <string>
using namespace std;

int main()
{
string line="qwer& &qwer&& &&";
line=line.replace(line.find("&"),1,"1"); //将line中的第一个&替换成1
cout<<line<<endl;
return 0;
}


//执行结果-- qwer1 &qwer&& &&
//___________________________________________________________________________________




#include<iostream>
#include<string>
using namespace std;
int main()
{
string line="qwer& &qwer&& &&";
line=line.replace(line.begin(),line.begin()+6,"1");//将line从begin位置开始的6个字符替换成1
cout<<line<<endl;
return 0;
}



//执行结果-- 1&qwer&& &&
//___________________________________________________________________________________



//用法三:用substr的指定子串(给定起始位置和长度)替换从指定位置上的字符串
nclude <iostream>
#include <string>
using namespace std;
int main()
{
string line="qwer& &qwer&& &&";
string substr="012345";
//将line字符串0到5位置上的字符替换为substr的指定子串
//从'1'位置开始的3个字符
line=line.replace(0,5,substr,substr.find("1"),3);
cout<<line<<endl;
return 0;
}

//执行结果-- 123 &qwer&& &&
//___________________________________________________________________________________



//用法四:用str替换大串指定位置上的子串(string转char*时编译器可能会爆出警告,不建议这样做)
#include <iostream>
#include <string>
using namespace std;
int main()
{
string line="qwer& &qwer&& &&";
char* str="012345";
//用str替换从指定位置0开始长度为5的字符串
line=line.replace(0,5,str);
cout<<line<<endl;
return 0;
}

//执行结果-- 012345 &qwer&& &&
//___________________________________________________________________________________



//用法五:用str替换从指定迭代器位置的字符串(string转char*时编译器可能会爆出警告,不建议这样做)

#include<iostream>
#include<string>
using namespace std;
int main()
{
string line="qwer& &qwer&& &&";
char* str="012345";
line=line.replace(line.begin(),line.begin()+9,str);
cout<<line<<endl;
return 0;
}

//执行结果-- 012345er&& &&
//___________________________________________________________________________________



//用法六:用s的前n个字符替换从开始位置pos长度为len的字符串,(string转char *时的编译器可能会爆出警告,不建议这样做

#include <iostream>
#include <string>
using namespace std;

int main()
{
string line="qwer& &qwer&& &&";
char* str="012345";
line=line.replace(0,9,str,5);
cout<<line<<endl;
return 0;
}


//执行结果-- 01234er&& &&
//___________________________________________________________________________________



//用法七:用s的前n个字符替换成指定迭代器位置(从i1到i2)的字符串,(string转char*时编译器可能会爆出警告,不建议这样做)
#include <iostream>
#include <string>
using namespace std;
int main()
{
string line="qwer& &qwer&& &&";
char* str="012345";
line=line.replace(line.begin(),line.begin()+9,str,5);
cout<<line<<endl;
return 0;
}


//执行结果-- 01234er&& &&
//___________________________________________________________________________________



//用法八:用重复n次的c字符替换从指定位置pos长度为len的内容
#include <iostream>
#include <string>
using namespace std;

int main()
{
string line="qwer& &qwer&& &&";
char c='1';
line=line.replace(0,9,3,c); //用重复3次的c字符替换从指定位置0 长度为9的内容
cout<<line<<endl;
return 0;
}
//执行结果-- 111er&& &&
//___________________________________________________________________________________



//用法9:用重复n次的c字符替换从指定迭代器位置(从i1开始到结束)的内容
#include <iostream>
#include <string>
using namespace std;
int main()
{
string line="qwer& &qwer&& &&";
char c='1';
line=line.replace(line.begin(),line.begin()+9,3,c);
cout<<line<<endl;
return 0;
}
//执行结果-- 111er&& &&
//___________________________________________________________________________________