The C ++ replace () function returns the maximum number of elements that a string can put. (Unlike capacity)
Size _ type max _ size () const; basic_string <char >:: size_type cap, max; cap
= S. capacity (); max = s. max_size (); // max= 4294967294.
Basic_string: rfind. Returns the first string value. If no value is found
Then npos is returned.
Unlike find, rfind starts from npos by default. Others are the same.
Basic_string: replace to replace the element or substring in the original string. Returns the string after replacement.
# Include <iostream>
# Include <string>
Using namespace std;
Int main ()
{
String str1 ("String handling C ++ style .");
String str2 ("STL Power ");
Cout <"Initial strings: n ";
Cout <"str1:" <str1 <endl;
Cout <"str2:" <str2 <"nn ";
// Demonstrate replace
Cout <"Replace 8 characters in str1 with str2: n ";
Str1.replace (7, 8, str2 );
Cout <str1 <endl;
Return 0;
}
/*
Initial strings:
Str1: String handling C ++ style.
Str2: STL Power
Replace 8 characters in str1 with str2:
String STL Power C ++ style.
*/
Specify location
# Include <iostream>
Using std: cout;
Using std: endl;
# Include <string>
Using std: string;
Int main ()
{
String string1 ("abc edfgh ijk lmno pqr stu vw xyz ");
Cout <"Original string: n" <string1 <endl;
Int position = string1.find ("."); // find first period
While (position! = String: npos)
{
String1.replace (position, 2, "12345; 123", 5, 2 );
Position = string1.find (".", position + 1 );
}
Cout <string1 <endl;
Return 0;
}
/*
Original string:
Abc edfgh ijk lmno pqr stu vw xyz
Abc edfgh ijk lmno pqr stu vw xyz
*/
Insert (), erase (), and replace ().
# Include <iostream>
# Include <string>
Using namespace std;
Int main ()
{
String str1 ("");
String str2 ("B ");
Cout <"Initial strings: n ";
Cout <"str1:" <str1 <endl;
Cout <"str2:" <str2 <"nn ";
Str1.insert (6, str2 );
Cout <str1 <"nn ";
Str1.erase (6, 9 );
Cout <str1 <"nn ";
Str1.replace (7, 8, str2 );
Cout <str1 <endl;
Return 0;
}