replace函数

/*
用正常方法 
#include <iostream>
#include <string>
using namespace std;
int main() {
	string Oldstr
	string str = "我是渣渣辉,我上八十年级";
	cout<<str<<endl; 
	str = str.replace(str.find("渣"),6,"王一行");  //从第一个(渣)的位置开始的三个汉字替换成(王一行)
	str = str.replace(str.find("八"),4,"五");  //从第一个(八)的位置开始的两个汉字替换成(五)
	//replace函数的 
	cout<<str;
	return 0;
}

用begin 
#include <iostream>
#include <string>
using namespace std;
int main() {
	string str = "he is@ a@ good boy";
	//cout<<str<<endl; 
	str = str.replace(str.begin(),str.begin()+5,"#");  //从#替换从begin位置开始的5个字符 
	cout<<str<<endl;
	return 0;
}

用指针 
#include <iostream>
#include <string>
using namespace std;
int main() {
	string str = "he is@ a@ good boy";
	char *str1 = "12345";
	str = str.replace(0,5,str1);  //从#替换从begin位置开始的5个字符 
	cout<<str<<endl;
	return 0;
}*/


#include <iostream>
#include <string>
using namespace std;
int main() {
	string str = "he is@ a@ good boy";
	char *str1 = "12345";
	str = str.replace(0,6,str1,4);  //用str1替换从begin位置开始的5个字符 
	cout<<str<<endl;
	return 0;
}

  

posted @ 2022-10-29 20:20  王一行  阅读(99)  评论(0)    收藏  举报