String operations-c ++

Source: Internet
Author: User

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

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.