C + + replace () functional usage detailed __jquery

Source: Internet
Author: User

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

[CPP] view plain copy/* Usage One: * Replace the specified string with str the character *string& replace (size_t pos, size_t Len, from the starting position of POS)   Const string& STR);       * * 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 Results:

[CPP] view plain Copy/* Usage two: * Replaces the character *string& replace of the iterator starting and ending positions with str (const_iterator I1, Const_iter   Ator 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 Results:


[CPP] view plain copy/* Usage three: * Replace the string from the specified position with the substr specified substring (given starting position and length) *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 Results:

[CPP]   View plain  copy/* Usage four: The compiler may report a warning when a string is char*, not recommended    * Replaces the string    *string& replace (size_t pos, size_t len, const) with STR starting at the specified position 0 start length of 5  char* s);   */   int main ()    {        string line =  "this@ is@ a test string!";        char* str =  "12345";        Line = line.replace (0,&NBSP;5,&NBSP;STR)  //use STR to replace a string with a length of 0 starting at the specified position of 5         cout << line << endl;     

Related Article

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.