1 Basic usage
(1) header file #include<string>
(2) Direct assignment, String str;str= "Hello my dear";
Assigns a character pointer to a string object: Char ss[30]= "my Name"; String Str=ss;
(3) Trailing append: string str= "Hello"; str+= ' A ';(plus character) str+= "AA";(plus string);
Str.append ("AAA");(method appended)
(4) Insert character: string str= "12345"; string::iterator it=str.begin ();
Str.insert (it+1, ' a ');//insert in front of the 1th element (starting from 0)
(5) Access: string str= "1234";cout<<str[0]<<str[1]<<endl;
(6) Delete: string str= "123456"; string::iterator it=str.begin (); str.erase (it); Delete ' 1 '
Str.erase (it+1,it+2);//delete ' 3 '
(7) Length str.length (); Determine whether it is an empty str.empty ();
(8) Replacement: Str.replace (I,len, "AAAA");//Starting with the first, the consecutive Len characters are replaced with "AAAA"; public 10
Overloaded versions, which are most commonly used.
(9) Find: int i=str.find ("AAA"); Find the position of "AAA" in Str, can not find return string::npos;
(10) Compare Str.compare ("AAA"); if str< "AAA", return-1;
str== "AAA", return 0;str> "AAA", return 1
(11) Flip, add header file #include<algorithm>; Reverse (Str.begin (), Str.end ());
2 string and number conversion
If a number is stored in a string, it can be traversed to process each digit
For (I=0;i<str.length (); i++) to str[i] processing (such as for each bit and, to an integer, etc.)
For more relevant articles, please follow the PHP Chinese web (www.php.cn)!