📅  最后修改于: 2020-10-20 08:28:51             🧑  作者: Mango
此函数替换从字符位置pos开始并跨越len个字符的字符串部分。
考虑两个字符串str1和str2。语法为:
str1.replace(pos,len,str2);
此函数不返回任何值。
第一个示例显示如何通过使用位置和长度作为参数来替换给定的字符串。
#include
using namespace std;
int main()
{
string str1 = "This is C language";
string str2 = "C++";
cout << "Before replacement, string is :"<
输出:
Before replacement , string is This is C language
After replacement, string is This is C++ language
第二个示例显示了如何使用要复制到另一个字符串对象中的字符串的位置和长度替换给定的字符串。
#include
using namespace std;
int main()
{
string str1 ="This is C language"
string str3= "java language";
cout <<"Before replacement, String is "<
输出:
Before replacement, String is This is C language
After replacement, String is This is java language
第三个示例显示如何通过使用字符串和要复制的字符数作为参数来替换字符串。
#include
using namespace std;
int main()
{
string str1="This is C language";
cout<<"Before replacement,string is"<
输出:
Before replacement,string is This is C language
After replacement,string is This is C# language