logo

C++ String replace()

Ta funkcija nadomesti del niza, ki se začne na položaju znaka pos in obsega len znakov.

Sintaksa

Razmislite o dveh nizih str1 in str2. Sintaksa bi bila:

 str1.replace(pos,len,str2); 

Parametri

    str:str je objekt niza, katerega vrednost je treba kopirati v drug objekt niza.položaj:pos definira pozicijo, katere znak je treba zamenjati.samo:Število znakov, ki jih je treba zamenjati z drugim objektom niza.podstavki:Določa položaj prvega znaka objekta niza, ki ga je treba kopirati v drug objekt kot zamenjavo.sublen :Število znakov nizovnega objekta, ki jih je treba kopirati v drug nizovni objekt.n:Število znakov, ki jih je treba kopirati v drug objekt niza.

Povratna vrednost

Ta funkcija ne vrne nobene vrednosti.

Primer 1

Prvi primer prikazuje, kako zamenjati dani niz z uporabo položaja in dolžine kot parametrov.

 #include using namespace std; int main() { string str1 = &apos;This is C language&apos;; string str2 = &apos;C++&apos;; cout &lt;&lt; &apos;Before replacement, string is :&apos;&lt;<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 =&apos;This is C language&apos; string str3= &apos;java language&apos;; cout &lt;<'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=&apos;This is C language&apos;; cout&lt;<'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<<'
';>

Primer 2

Drugi primer prikazuje, kako zamenjati dani niz z uporabo položaja in dolžine niza, ki ga želite kopirati v drug objekt niza.

 #include using namespace std; int main() { string str1 =&apos;This is C language&apos; string str3= &apos;java language&apos;; cout &lt;<\'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=&apos;This is C language&apos;; cout&lt;<\'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>

Primer 3

Tretji primer prikazuje, kako zamenjati niz z uporabo niza in števila znakov, ki jih je treba kopirati kot parametrov.

 #include using namespace std; int main() { string str1=&apos;This is C language&apos;; cout&lt;<\'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>