Aquesta funció substitueix la part de la cadena que comença a la posició del caràcter pos i abasta els caràcters len.
Sintaxi
Considereu dues cadenes str1 i str2. La sintaxi seria:
str1.replace(pos,len,str2);
Paràmetres
Valor de retorn
Aquesta funció no retorna cap valor.
Exemple 1
El primer exemple mostra com substituir una cadena donada utilitzant la posició i la longitud com a paràmetres.
#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<<' ';>
Exemple 2
El segon exemple mostra com substituir una cadena donada utilitzant la posició i la longitud de la cadena que s'ha de copiar en un altre objecte de cadena.
#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>
Exemple 3
El tercer exemple mostra com substituir la cadena utilitzant la cadena i el nombre de caràcters a copiar com a paràmetres.
#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>