Summary of basic application methods of C ++ replace () Functions

Source: Internet
Author: User

The string application methods in the C ++ programming language are diversified. Each application method can help us to meet specific functional requirements. Here we will give you a detailed description of one of the important usage, the application method of the C ++ replace () function.

 
 
  1. basic_string::max_size  

The C ++ replace () function returns the maximum number of elements that a string can put. Different from capacity)

 
 
  1. size _ type max _ size( ) const;   
  2. basic_string <char>::size_type cap, max;   
  3. cap = s.capacity ( );   
  4. max = s.max_size ( ); // max=4294967294.   
  5. basic_string::rfind  

Search for the given string. Returns the first string value. If no value is found, npos is returned.

Unlike find, rfind starts from npos by default. Others are the same.

 
 
  1. basic_string::replace  

Replace the element or substring in the original string. Returns the string after replacement.

1) Use string or C-string to replace the _ Num1 character starting from _ Pos1 in the operation string.

 
 
  1. basic _ string& replace( size _ type _Pos1 ,
    size _ type _Num1 , const value _ type* _Ptr );   
  2. basic _ string& replace(size _ type _Pos1 ,
    size _ type _Num1 ,const basic _ string _Str );   
  3. string a,b;   
  4. string s ( "AAAAAAAA" );   
  5. string s1p ( "BBB" );   
  6. const char* cs1p = "CCC" ;   
  7. a = s.replace ( 1 , 3 , s1p ); // s= ” ABBBAAAA ”   
  8. b = s.replace ( 5 , 3 , cs1p ); // s= ” ABBBACCC ”  

2) replace _ Num1 characters starting from _ Pos2 in the C ++ replace () function

Use _ Num2 characters in C-string to replace _ Num1 characters starting from _ Pos1 in string.

 
 
  1. basic _ string& replace( size _ type _Pos1 , 
    size _ type _Num1 , const basic _ string& _Str ,   
  2. size _ type _Pos2 , size _ type );   
  3. basic _ string& replace( size _ type _Pos1 , size _ type _Num1 ,   
  4. const value _ type* _Ptr , size _ type _Num2 );   
  5. string a, b;   
  6. string s ( "AAAAAAAA" );   
  7. string s2p ( "BBB" );   
  8. const char* cs2p = "CCC";   
  9. a = s.replace ( 1 , 3 , s2p , 1 , 2 ); // s= ” ABBAAAA ”   
  10. b = s.replace ( 4 , 3 , cs2p , 1 ); // s= ” ABBAC ”  

3) Use _ Count character _ Ch to replace _ Num1 characters starting from _ Pos1 in the operation string.

 
 
  1. basic _ string& replace( size _ type _Pos1 , size _ type _Num1 ,   
  2. size _ type _Count , value _ type _Ch );   
  3. string result;   
  4. string s ( "AAAAAAAA" );   
  5. char ch = 'C';   
  6. result = s.replace ( 1 , 3 , 4 , ch ); // s= ” ACCCCAAAA ”  

4) use string or C-string to replace the characters from First0 to Last0 in the operation string.

 
 
  1. basic _ string&replace(iterator First0 ,iterator Last0 , 
    const basic _ string& _Str );   
  2. basic _ string&replace(iterator First0 ,iterator _Last0 , 
    const value _ type* _Ptr );   
  3. string s ( "AAAAAAAA" ); string s4p ( "BBB" );   
  4. const char* cs4p = "CCC";   
  5. basic_string<char>::iterator IterF0, IterL0;   
  6. IterF0 = s.begin ( ); IterL0 = s.begin ( ) + 3;   
  7. string a, b;   
  8. a = s.replace ( IterF0 , IterL0 , s4p ); // s= ” BBBAAAAA ”   
  9. b = s.replace ( IterF0 , IterL0 , cs4p ); // s= ” CCCAAAAA ”  

5) use the _ Num2 characters starting from _ Pos2 in the C ++ replace () function to replace the characters from First0 to Last0 in the operation string.

Use the _ Num2 characters in the C-string to replace the characters from First0 to Last0 in the operation string.

 
 
  1. basic _ string& replace( iterator _First0 , iterator _Last0 ,   
  2. const value _ type* _Ptr , size _ type _Num2 );   
  3. template<class InputIterator> basic _ string& replace(   
  4. iterator _First0 , iterator _Last0 ,   
  5. InputIterator _First , InputIterator _Last );   
  6. IterF3 = s.begin ( ) + 1; IterL3 = s.begin ( ) + 3;   
  7. IterF4 = s.begin ( ); IterL4 = s.begin ( ) + 2;   
  8. a = s.replace ( IterF3 , IterL3 , IterF4 , IterL4 );   
  9. b = s.replace ( IterF1 , IterL1 , cs5p , 4 );  

6) Use _ Count character _ Ch to replace the characters from First0 to Last0 in the operation string.

 
 
  1. basic _ string& replace( iterator _First0 , iterator _Last0 ,   
  2. size _ type _Count , value _ type _Ch );   
  3. a = s.replace ( IterF2 , IterL2 , 4 , ch );   
  4. basic_string::swap  

Exchange two strings.

 
 
  1. void swap( basic _ string& _Str );   
  2. s1.swap ( s2 );   
  3. basic_string::substr  

Returns a string consisting of _ Count characters starting with _ Off (subscript ).

 
 
  1. basic _ string substr( size _ type _Off = 0, 
    size _ type _Count = npos ) const;   
  2. string s("I love you!") , sub;   
  3. ssub=s.substr( ); // sub= ” I love you! ”   
  4. ssub=s.substr(1); // sub= ” love you! ”   
  5. ssub=s.substr(3,4); // sub= ” ove ”  

The content of the C ++ replace () function is described here.

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.