استبدال سلسلة C++ ()

تحل هذه الوظيفة محل جزء السلسلة الذي يبدأ عند موضع الحرف pos ويمتد إلى أحرف len.

بناء الجملة

النظر في سلسلتين str1 وstr2. بناء الجملة سيكون:

 str1.replace(pos,len,str2);  

حدود

    شارع : str هو كائن سلسلة سيتم نسخ قيمته في كائن سلسلة آخر. نقاط البيع : يحدد pos الموضع الذي سيتم استبدال شخصيته. فقط : عدد الأحرف المطلوب استبدالها بكائن سلسلة آخر. الافتراضات الفرعية: فهو يحدد موضع الحرف الأول من كائن السلسلة الذي سيتم نسخه إلى كائن آخر كبديل. سوبلين : عدد أحرف كائن السلسلة المراد نسخها إلى كائن سلسلة آخر. ن : عدد الأحرف المطلوب نسخها إلى كائن سلسلة آخر.

قيمة الإرجاع

هذه الدالة لا ترجع أي قيمة.

مثال 1

يوضح المثال الأول كيفية استبدال السلسلة المعطاة باستخدام الموضع والطول كمعلمات.

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

مثال 2

يوضح المثال الثاني كيفية استبدال سلسلة معينة باستخدام موضع وطول السلسلة التي سيتم نسخها في كائن سلسلة آخر.

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

مثال 3

يوضح المثال الثالث كيفية استبدال السلسلة باستخدام السلسلة وعدد الأحرف المراد نسخها كمعلمات.

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