Ova funkcija zamjenjuje dio niza koji počinje na položaju poz i obuhvaća len znakova.
Sintaksa
Razmotrimo dva niza str1 i str2. Sintaksa bi bila:
str1.replace(pos,len,str2);
Parametri
Povratna vrijednost
Ova funkcija ne vraća nikakvu vrijednost.
Primjer 1
Prvi primjer pokazuje kako zamijeniti dati niz pomoću položaja i duljine kao parametara.
#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<<' ';>
Primjer 2
Drugi primjer pokazuje kako zamijeniti zadani niz pomoću položaja i duljine niza koji se želi kopirati u drugi objekt niza.
#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>
Primjer 3
Treći primjer pokazuje kako zamijeniti niz korištenjem niza i broja znakova koji se kopiraju kao parametri.
#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>