A detailed description of the C + + replace () function usage

Source: Internet
Author: User

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

[CPP]View Plaincopy
  1. /* Usage One:
  2. * Replace the specified string with str from the start position POS starting at the length Len character
  3. *string& Replace (size_t pos, size_t len, const string& str);
  4. */
  5. int main ()
  6. {
  7. String line = "[email protected] [email protected] a test string!";
  8. line = Line.replace (Line.find ("@"), 1, ""); //Replace the first @ position with a null
  9. cout << line << Endl;
  10. return 0;
  11. }

Operation Result:

[CPP]View Plaincopy
  1. /* Usage Two:
  2. * Replace the character of the iterator's starting and ending positions with STR
  3. *string& Replace (const_iterator i1, const_iterator i2, const string& str);
  4. */
  5. int main ()
  6. {
  7. String line = "[email protected] [email protected] a test string!";
  8. line = Line.replace (Line.begin (), Line.begin () +6, ""); //replace 6 characters from begin position with STR
  9. cout << line << Endl;
  10. return 0;
  11. }

Operation Result:

[CPP]View Plaincopy
  1. /* Use three:
  2. * Replace the string from the specified position with the specified substring of substr (given start position and length)
  3. *string& Replace (size_t pos, size_t len, const string& STR, size_t subpos, size_t Sublen);
  4. */
  5. int main ()
  6. {
  7. String line = "[email protected] [email protected] a test string!";
  8. String substr = "12345";
  9. line = Line.replace (0, 5, substr, Substr.find ("1"), 3); Replace line from 0 to 5 positions with the specified substring of substr (3 characters from 1 digits)
  10. cout << line << Endl;
  11. return 0;
  12. }

Operation Result:

[CPP]View Plaincopy
  1. /* Usage Four: The compiler may report a warning when string goes char*, it is not recommended
  2. * Replace the string with a length of 5 from the specified position 0 with str
  3. *string& Replace (size_t pos, size_t len, const char* s);
  4. */
  5. int main ()
  6. {
  7. String line = "[email protected] [email protected] a test string!";
  8. char* str = "12345";
  9. line = Line.replace (0, 5, str); //Replace with str a string of length 5 starting at the specified position 0
  10. cout << line << Endl;
  11. return 0;
  12. }

Operation Result:

[CPP]View Plaincopy
  1. /* Usage Five: The compiler may report a warning when string goes char*, it is not recommended
  2. * Replace the string from the specified iterator position with STR
  3. *string& Replace (const_iterator i1, const_iterator i2, const char* s);
  4. */
  5. int main ()
  6. {
  7. String line = "[email protected] [email protected] a test string!";
  8. char* str = "12345";
  9. line = Line.replace (Line.begin (), Line.begin () +9, str); //Replace the string from the specified iterator position with str
  10. cout << line << Endl;
  11. return 0;
  12. }

Operation Result:

[CPP]View Plaincopy
  1. /* Usage Six: The compiler may report a warning when string goes char*, it is not recommended
  2. * Replace the first n characters of s with a string from the start position of the POS length len
  3. *string& Replace (size_t pos, size_t len, const char* S, size_t N);
  4. */
  5. int main ()
  6. {
  7. String line = "[email protected] [email protected] a test string!";
  8. char* str = "12345";
  9. line = Line.replace (0, 9, str, 4); //Replace string with length 9 starting at 0 position with first 4 characters of Str
  10. cout << line << Endl;
  11. return 0;
  12. }

Operation Result:

[CPP]View Plaincopy
  1. /* Usage Seven: The compiler may report a warning when string char* is not recommended
  2. * Replace the string of the specified iterator position (from I1 to I2) with the first n characters of S
  3. *string& Replace (const_iterator i1, const_iterator i2, const char* S, size_t N);
  4. */
  5. int main ()
  6. {
  7. String line = "[email protected] [email protected] a test string!";
  8. char* str = "12345";
  9. line = Line.replace (Line.begin (), Line.begin () +9, str, 4); //Replace a string with the first 4 characters of STR for the position of the specified iterator
  10. cout << line << Endl;
  11. return 0;
  12. }

Operation Result:

[CPP]View Plaincopy
  1. /* Usage Eight:
  2. * Replace the contents of the POS length len from the specified position with the C character repeated n times
  3. *string& Replace (size_t pos, size_t len, size_t N, char c);
  4. */
  5. int main ()
  6. {
  7. String line = "[email protected] [email protected] a test string!";
  8. char c = ' 1 ';
  9. line = Line.replace (0, 9, 3, C); //Replace the contents of 0 length 9 with the C character repeated 3 times from the specified position
  10. cout << line << Endl;
  11. return 0;
  12. }

Operation Result:

[CPP]View Plaincopy
  1. /* Usage IX:
  2. * Replace the contents of the specified iterator position (starting from I1 to the end) with a C character that repeats n times
  3. *string& Replace (const_iterator i1, Const_iterator i2, size_t N, char c);
  4. */
  5. int main ()
  6. {
  7. String line = "[email protected] [email protected] a test string!";
  8. char c = ' 1 ';
  9. line = Line.replace (Line.begin (), Line.begin () +9, 3, C); //Replace the content from the specified iterator position with a C character that repeats 3 times
  10. cout << line << Endl;
  11. return 0;
  12. }

Operation Result:

(go) C + + replace () function usage

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.