In this paper, we mainly give a sample program for the use of Replace function in C + +
[CPP]View Plaincopy
- /* Usage One:
- * Replace the specified string with str from the start position POS starting at the length Len character
- *string& Replace (size_t pos, size_t len, const string& str);
- */
- int main ()
- {
- String line = "[email protected] [email protected] a test string!";
- line = Line.replace (Line.find ("@"), 1, ""); //Replace the first @ position with a null
- cout << line << Endl;
- return 0;
- }
Operation Result:
[CPP]View Plaincopy
- /* Usage Two:
- * Replace the character of the iterator's starting and ending positions with STR
- *string& Replace (const_iterator i1, const_iterator i2, const string& str);
- */
- int main ()
- {
- String line = "[email protected] [email protected] a test string!";
- line = Line.replace (Line.begin (), Line.begin () +6, ""); //replace 6 characters from begin position with STR
- cout << line << Endl;
- return 0;
- }
Operation Result:
[CPP]View Plaincopy
- /* Use three:
- * Replace the string from the specified position with the specified substring of substr (given start position and length)
- *string& Replace (size_t pos, size_t len, const string& STR, size_t subpos, size_t Sublen);
- */
- int main ()
- {
- String line = "[email protected] [email protected] a test string!";
- String substr = "12345";
- 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)
- cout << line << Endl;
- return 0;
- }
Operation Result:
[CPP]View Plaincopy
- /* Usage Four: The compiler may report a warning when string goes char*, it is not recommended
- * Replace the string with a length of 5 from the specified position 0 with str
- *string& Replace (size_t pos, size_t len, const char* s);
- */
- int main ()
- {
- String line = "[email protected] [email protected] a test string!";
- char* str = "12345";
- line = Line.replace (0, 5, str); //Replace with str a string of length 5 starting at the specified position 0
- cout << line << Endl;
- return 0;
- }
Operation Result:
[CPP]View Plaincopy
- /* Usage Five: The compiler may report a warning when string goes char*, it 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 = "[email protected] [email protected] a test string!";
- char* str = "12345";
- line = Line.replace (Line.begin (), Line.begin () +9, str); //Replace the string from the specified iterator position with str
- cout << line << Endl;
- return 0;
- }
Operation Result:
[CPP]View Plaincopy
- /* Usage Six: The compiler may report a warning when string goes char*, it is not recommended
- * Replace the first n characters of s with a string from the start position of the POS length len
- *string& Replace (size_t pos, size_t len, const char* S, size_t N);
- */
- int main ()
- {
- String line = "[email protected] [email protected] a test string!";
- char* str = "12345";
- line = Line.replace (0, 9, str, 4); //Replace string with length 9 starting at 0 position with first 4 characters of Str
- cout << line << Endl;
- return 0;
- }
Operation Result:
[CPP]View Plaincopy
- /* Usage Seven: The compiler may report a warning when string char* is not recommended
- * Replace the string of the specified iterator position (from I1 to I2) with the first n characters of S
- *string& Replace (const_iterator i1, const_iterator i2, const char* S, size_t N);
- */
- int main ()
- {
- String line = "[email protected] [email protected] a test string!";
- char* str = "12345";
- 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
- cout << line << Endl;
- return 0;
- }
Operation Result:
[CPP]View Plaincopy
- /* Usage Eight:
- * Replace the contents of the POS length len from the specified position with the C character repeated n times
- *string& Replace (size_t pos, size_t len, size_t N, char c);
- */
- int main ()
- {
- String line = "[email protected] [email protected] a test string!";
- char c = ' 1 ';
- 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
- cout << line << Endl;
- return 0;
- }
Operation Result:
[CPP]View Plaincopy
- /* Usage IX:
- * Replace the contents of the specified iterator position (starting from I1 to the end) with a C character that repeats n times
- *string& Replace (const_iterator i1, Const_iterator i2, size_t N, char c);
- */
- int main ()
- {
- String line = "[email protected] [email protected] 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 C character that repeats 3 times
- cout << line << Endl;
- return 0;
- }
Operation Result:
(go) C + + replace () function usage