Bu işlev, dizenin karakter konumunda başlayan ve len karakterleri kapsayan kısmını değiştirir.
Sözdizimi
str1 ve str2 şeklinde iki dizi düşünün. Sözdizimi şöyle olacaktır:
str1.replace(pos,len,str2);
Parametreler
Geri dönüş değeri
Bu fonksiyon herhangi bir değer döndürmez.
örnek 1
İlk örnek, parametre olarak konum ve uzunluk kullanılarak verilen dizenin nasıl değiştirileceğini gösterir.
#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<<' ';>
Örnek 2
İkinci örnek, başka bir dize nesnesine kopyalanacak olan dizenin konumu ve uzunluğu kullanılarak verilen dizenin nasıl değiştirileceğini gösterir.
#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>
Örnek 3
Üçüncü örnek, parametre olarak kopyalanacak karakter sayısını ve dizeyi kullanarak dizenin nasıl değiştirileceğini gösterir.
#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>