Тази функция замества частта от низ, която започва от позицията на знака pos и обхваща len символи.
Синтаксис
Разгледайте два низа str1 и str2. Синтаксисът би бил:
str1.replace(pos,len,str2);
Параметри
Върната стойност
Тази функция не връща никаква стойност.
Пример 1
Първият пример показва как да замените даден низ, като използвате позиция и дължина като параметри.
#include using namespace std; int main() { string str1 = 'This is C language'; string str2 = 'C++'; cout << 'Before replacement, string is :'<<str1<<' '; str1.replace(8,1,str2); cout << 'after replacement, string is :'<<str1<<' '; return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> Before replacement , string is This is C language After replacement, string is This is C++ language </pre> <h2>Example 2</h2> <p>Second example shows how to replace given string using position and length of the string which is to be copied in another string object.</p> <pre> #include using namespace std; int main() { string str1 ='This is C language' string str3= 'java language'; cout <<'before replacement, string is '<<str1<<' '; str1.replace(8,1,str3,0,4); cout<<'after replacement,string return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> Before replacement, String is This is C language After replacement, String is This is java language </pre> <h2>Example 3</h2> <p>Third example shows how to replace the string by using string and number of characters to be copied as parameters.</p> <pre> #include using namespace std; int main() { string str1='This is C language'; cout<<'before replacement,string is'<<str1<<' '; str1.replace(8,1,'c##',2); cout<<'after is'<<str1; return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> Before replacement,string is This is C language After replacement,string is This is C# language </pre> <br></'before></pre></'before></pre></str1<<' ';>
Пример 2
Вторият пример показва как да замените даден низ, като използвате позицията и дължината на низа, който трябва да бъде копиран в друг обект на низ.
#include using namespace std; int main() { string str1 ='This is C language' string str3= 'java language'; cout <<\'before replacement, string is \'<<str1<<\' \'; str1.replace(8,1,str3,0,4); cout<<\'after replacement,string return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> Before replacement, String is This is C language After replacement, String is This is java language </pre> <h2>Example 3</h2> <p>Third example shows how to replace the string by using string and number of characters to be copied as parameters.</p> <pre> #include using namespace std; int main() { string str1='This is C language'; cout<<\'before replacement,string is\'<<str1<<\' \'; str1.replace(8,1,\'c##\',2); cout<<\'after is\'<<str1; return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> Before replacement,string is This is C language After replacement,string is This is C# language </pre> <br></\'before></pre></\'before>
Пример 3
Третият пример показва как да замените низа, като използвате низ и брой символи, които да бъдат копирани като параметри.
#include using namespace std; int main() { string str1='This is C language'; cout<<\'before replacement,string is\'<<str1<<\' \'; str1.replace(8,1,\'c##\',2); cout<<\'after is\'<<str1; return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> Before replacement,string is This is C language After replacement,string is This is C# language </pre> <br></\'before>
\'before>\'before>