logo

C++ String replace()

Denne funktion erstatter den del af strengen, der begynder ved tegnposition pos og spænder over len-tegn.

Syntaks

Overvej to strenge str1 og str2. Syntaks ville være:

 str1.replace(pos,len,str2); 

Parametre

    str :str er et strengobjekt, hvis værdi skal kopieres i et andet strengobjekt.pos:pos definerer den position, hvis karakter skal erstattes.kun:Antal tegn, der skal erstattes af et andet strengobjekt.underordnede:Den definerer placeringen af ​​det første tegn i strengobjektet, der skal kopieres til et andet objekt som erstatning.sublen:Antal tegn i strengobjekt, der skal kopieres til et andet strengobjekt.n:Antal tegn, der skal kopieres til et andet strengobjekt.

Returværdi

Denne funktion returnerer ingen værdi.

Eksempel 1

Første eksempel viser, hvordan man erstatter en given streng ved at bruge position og længde som parametre.

 #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<<'
';>

Eksempel 2

Andet eksempel viser, hvordan man erstatter en given streng ved hjælp af position og længde af strengen, som skal kopieres i et andet strengobjekt.

 #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>

Eksempel 3

Tredje eksempel viser, hvordan man erstatter strengen ved at bruge streng og antal tegn, der skal kopieres som parametre.

 #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>