On the Replace () method in C + + _c language

Source: Internet
Author: User

In this paper, we mainly give nine sample programs for the use of Replace function in C + +:

Usage One:


 * * Replaces the specified string with str with the character 
 *string& replace (size_t pos, size_t len, const string& str) Starting at the beginning of the POS length as Len; 
 * * 
int main () 
{string Line 
 = "this@ is@ a Test string!"; 
 line = Line.replace (Line.find ("@"), 1, ""); Replace the first @ for empty 
 cout << line << Endl from the first @ position; 
 return 0; 
} 

Run Result:

Usage Two:


 * * Replace the character *string& replace with Str at the start and end of the iterator 
 (const_iterator i1, const_iterator i2, const string& str) ; 
 * * 
int main () 
{string Line 
 = "this@ is@ a Test string!"; 
 line = Line.replace (Line.begin (), Line.begin () +6, ""); Replaces 6 characters 
 cout << line << Endl with str starting from begin position; 
 return 0; 
} 

Run Result:

Usage Three:


 * * Replace the string from the specified position with the specified substring (given starting position and length) of substr 
 *string& replace (size_t pos, size_t len, const string& STR, size_t Subpos, size_t sublen); 
 * * 
int main () 
{string Line 
 = "this@ is@ a Test string!"; 
 String substr = "12345"; 
 line = Line.replace (0, 5, substr, Substr.find ("1"), 3); Replaces line 
 cout << line << Endl from 0 to 5 positions with the specified substring of the substr (from 1 digits of 3 characters); 
 return 0; 
} 

Run Result:

Usage IV:The compiler may report a warning when char* is not recommended


 * * Replace the string with str starting at the specified position of 0 with a length of 5 
 *string& replace (size_t pos, size_t len, const char* s); 
 * * 
int main () 
{string Line 
 = "this@ is@ a Test string!"; 
 char* str = "12345"; 
 line = Line.replace (0, 5, str); Replaces the string 
 cout << line << Endl with Str with a start length of 5 from the specified position 0; 
 return 0; 
} 


Run Result:

usage Five:string The compiler may report a warning when char* is not recommended


 * * Replace the string from the specified iterator position with str 
 *string& replace (const_iterator i1, const_iterator i2, const char* s); 
 * * 
int main () 
{string Line 
 = "this@ is@ a Test string!"; 
 char* str = "12345"; 
 line = Line.replace (Line.begin (), Line.begin () +9, str); Replaces the string from the specified iterator position with str 
 cout << line << Endl; 
 return 0; 
} 

Run Result:

usage Six:the compiler may report a warning when a string is char*, not recommended


 /* * Replace the string 
 *string& replace (size_t pos, size_t len, const char* S, size_t N) with the first n characters of S, from the start position pos length len; 
 * * 
int main () 
{string Line 
 = "this@ is@ a Test string!"; 
 char* str = "12345"; 
 line = Line.replace (0, 9, str, 4); Replaces the string 
 cout << line << Endl with the first 4 characters of Str from a 0 position starting length of 9; 
 return 0; 
} 

Run Result:

usage Seven:string The compiler may report a warning when char* is not recommended


 * * Replaces the string with the first n characters of s with the specified iterator position (from I1 to I2) 
 *string& replace (const_iterator i1, const_iterator i2, const char* s, size_t n); 
 * * 
int main () 
{string Line 
 = "this@ is@ a Test string!"; 
 char* str = "12345"; 
 line = Line.replace (Line.begin (), Line.begin () +9, str, 4); Replaces the string at the specified iterator position with the first 4 characters of str 
 cout << line << Endl; 
 return 0; 
} 

Run Result:

usage Eight:

 

* * * Replace the contents of the POS length of Len with the repeated n-C characters 
 *string& replace (size_t pos, size_t len, size_t N, char c); 
 * * 
int main () 
{string Line 
 = "this@ is@ a Test string!"; 
 char c = ' 1 '; 
 line = Line.replace (0, 9, 3, C); Replace the content from the specified position 0 length 9 with a repeat of the C character of 3 times 
 cout << line << Endl; 
 return 0; 
} 

Run Result:

Usage Nine:


 * * Replace the contents of the specified iterator position (starting from i1 to end) with the repeated n C characters 
 *string& replace (const_iterator i1, Const_iterator i2, size_t N, char c); 
 * * 
int main () 
{string Line 
 = "this@ is@ a Test string!"; 
 char c = ' 1 '; 
 line = Line.replace (Line.begin (), Line.begin () +9, 3, C); Replace the content from the specified iterator position with a repeat of the C character of 3 times 
 cout << line << Endl; 
 return 0; 
} 


Run Result:

Note: All parameters that use an iterator type are not limited to string types, and can be a vector, list, and other type of iterator.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.