C ++ string. replace () character replacement function usage

Source: Internet
Author: User
Tags first string

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;
}


 

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.