Questa funzione sostituisce la porzione di stringa che inizia nella posizione del carattere pos e si estende su caratteri len.
Sintassi
Consideriamo due stringhe str1 e str2. La sintassi sarebbe:
str1.replace(pos,len,str2);
Parametri
Valore di ritorno
Questa funzione non restituisce alcun valore.
Esempio 1
Il primo esempio mostra come sostituire una determinata stringa utilizzando posizione e lunghezza come parametri.
#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<<' ';>
Esempio 2
Il secondo esempio mostra come sostituire una determinata stringa utilizzando la posizione e la lunghezza della stringa che deve essere copiata in un altro oggetto stringa.
#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>
Esempio 3
Il terzo esempio mostra come sostituire la stringa utilizzando la stringa e il numero di caratteri da copiare come 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>