logo

C++ String replace()

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

    str:str je string objekt čija se vrijednost kopira u drugi string objekt.položaj:pos definira poziciju čiji znak treba zamijeniti.samo:Broj znakova koji će se zamijeniti drugim objektom niza.potpostavke:Definira položaj prvog znaka string objekta koji se kopira u drugi objekt kao zamjena.sublen :Broj znakova string objekta koji će se kopirati u drugi string objekt.n :Broj znakova koji se kopiraju u drugi objekt niza.

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 = &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<<'
';>

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 =&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>

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=&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>