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;