C ++ STL String class learning (Continued)

Header: <String>

 

String class initialization:

String S1 = " aabbba " ; 
S2 = String ( " BBB " );  
 String S3 = String (S1);   // Hubuyingxiang

 

String Class basic operations:

    0, the string length is obtained

int len = s.length()

 

    1, the index

char C = s [i]; // slicing operation, each s [i] is character

   

    2, returns the substring (sliced)

// s.substr () function returns a substring 
S1 = s.substr ( 1 ) // from the subscript 1 is the last character to the 
S1 = s.substr (x, Y) // from the subscript x is a start character, a string of length y

 

    3, inserts the string

// use s.insert () function into a string 

S1 = s.insert ( 2 , " ABCD " ) // a subscript 2 is a front insertion character string

 

    4, delete some

String S; 
s.erase (x, y); / * Delete character starts from the subscript x is a string y. And returns the substring after deletion. * / 

S = " ABCDEFG " ; 
s.erase ( 2 , 3 ); // delete the character starts from index 2, a string of 3 

// S == "AbfG"

    

    5, the end of the string is inserted

String S = " ABCDE " ;
 String S1 = " thankyou " ;
 String S2 = "" ;
 // usage. 1: 
S2 = s.append ( " thankyou " ); 
S2 = s.append (S2); 
S2 = S1 + S ;   // value equal to the above three equations s2 

@ usage 2: 
s.append ( " howareyou " , 2 ) // "howareyou" s is added to the front end of the two characters 

@ usage. 3: 
s.append ( " howareyou " ,2, 3 ) / * From "howareyou" subscript character 2 is started, the addition of a string of length s. 3 (added after the string sections added to the original string)

 

    6, the replacement operation

s.replace does ( 1 , 3 , " ABC " )
  / * subscript subscript 1 to 3 substring of characters is replaced with ABC, if you want to replace a single character, you need to be replaced to write two characters subscript times such as: s.replace (1, 1, "abc") * /

 

Guess you like

Origin www.cnblogs.com/Eeyor/p/11233752.html
Recommended