/* 用正常方法 #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; }
标签:string,int,str1,cout,replace,str,include,函数 From: https://www.cnblogs.com/wangyihang/p/16839752.html