# Include <iostream>
# Include <string>
# Include <algorithm>
# Include <sstream>
# Include <vector>
Using namespace std;
Vector <string> split (const string & src, const string sp );
Int main (){
String str ("Hello, World ");
// 1. Obtain the first letter of the string
Cout <"1. Obtain the first letter of the string:" + str. substr (0, 1) <endl;
// 2. Obtain the second and third letters of the string
Cout <"2. Obtain the second and third letters of the string:" + str. substr (1, 2) <endl;
// 3. Obtain the last letter of the string
Cout <"3. Get the last letter of the string:" + str. substr (str. length ()-1, 1) <endl;
// 4. Start with a string
If (str. find ("Hello") = 0 ){
Cout <"4. String-start letter comparison: starting with Hello" <endl;
}
// 5. Judge the end of the string with letters
String w ("World ");
If (str. rfind (w) = str. length ()-w. length ()){
Cout <"5. Comparison of letters ending with a string: end with World" <endl;
}
// 6. Get the string length
Stringstream ss;
Ss <str. length ();
Cout <"6. Get String Length:" + ss. str () <endl;
// 7. case-insensitive Conversion
Transform (str. begin (), str. end (), str. begin (),: toupper );
Cout <"7. Case sensitivity conversion:" + str;
Transform (str. begin (), str. end (), str. begin (),: tolower );
Cout <"," + str <endl;
// 8. Convert string to int, int to string
Int num;
Stringstream ss2 ("100 ");
Ss2> num;
Stringstream ss3;
Ss3 <num;
Cout <"8. string to int, int to string:" + ss3.str () + "," + ss3.str () <endl;
// 9. Split the string
Vector <string> strs =: split (str, string (","));
Cout <"9. Split string: [" + strs [0] + "," + strs [1] <"]" <endl;
// 10. Determine whether the string contains
Str = "Hello, World ";
If (str. find ("o, W ")! =-1 ){
Cout <"10. Split string: Contains o, W" <endl;
}
Return 0;
}
Vector <string> split (const string & src, string sp ){
Vector <string> strs;
Int sp_len = sp. size ();
Int position = 0, index =-1;
While (-1! = (Index = src. find (sp, position ))){
Strs. push_back (src. substr (position, index-position ));
Position = index + sp_len;
}
String lastStr = src. substr (position );
If (! LastStr. empty ())
Strs. push_back (lastStr );
Return strs;
}