String data types in C + +

Source: Internet
Author: User

1, string

The initialization of string, in C + +, A string is a data type;

(1), initialization of string, traversal, string connection

The code is as Follows:

#include <iostream> #include <string> #include <stdio.h>using namespace std;int  Main (void) {  //string initialization, in C + + The string is a data type;    string s1 =  " ABCDEFG ";     string s2 (" abcdefg ");     string s3 (s2);     string s4 = s1;  //call Copy constructor;     string s5 ( 10,  ' A '); the characters in THE//10 space are ' a ';     s5 = s1;     cout << "s3:" <<s3<<endl;    cout<< "s5:" <<s5<<endl;// The traversal of string, overloading the [] operator,     //1, array Way traversal []    for (int i = 0;  i < s1.length ();  i++) {        cout<<s1[i ]<< " ",   //error (subscript out of bounds), will not be out of the exception, causing the program interruption;    }        cout<<endl;    //2, iterators     string::iterator it;      for (it = s1.begin ();  it != s1.end ();  it++) {         cout<<*it<< " ";    }    cout< <endl;    //3, function at () Traverse     for (int i = 0; i  < s1.length ();  i++) {        cout<<s1.at (i) << " ";  //will cut out abnormal, reasonable to solve the subscript out of bounds;     }    cout<<endl;// Character pointer and string conversion     //at this point, s1====>char *  the first address of the memory to reveal;     printf ("s1:%s \n",  s1.c_str ()),     //s1 content copied to buf;     char  buf[123] = {0};    s1.copy (buf, 2, 0);//n, pos; Subscript starting from 0 copy 2 characters to buf, will not be c-style, Note yourself plus 0 end sign; The connection     s1 = s1 + of the    cout<<buf<<endl;//string substring  s2; //direct + table: string connection;     s1 += s2; //+= is also a string connection;     s1.append (s4);  //call method Append () is also a string connection;    cout<<s1<<endl;            return 0;}

Operation Result:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/8A/B0/wKiom1g3j7jTmJwMAABN4l3uyzU176.png-wh_500x0-wm_3 -wmp_4-s_3916689983.png "title=" qq20161125091056.png "alt=" wkiom1g3j7jtmjwmaabn4l3uyzu176.png-wh_50 "/>

(2), string lookup, Replace

The code is as Follows:

#include <iostream> #include <string> #include <string.h>using namespace std;int  Main (void) {//string Find and replace     string s1 =  ' wbm hello wbm 111  wbm 222 wbm 333 ";     //1, First appearance wbm subscript     int  Index = s1.find ("wbm",  0);     cout<< "index :" <<index <<endl;    //2, WBM Each occurrence of the array subscript     /*  int offindex  = s1.find ("wbm",  0);     while (offindex != -1) {         cout<< "offindex :" <<offindex<<endl;         offindex += strlen ("wbm");         offindex = s1.find ("wbm",  offindex);     }*/    // 3. Replace the lowercase WBM with uppercase &NBSP;&NBSp;  int offindex = s1.find ("wbm",  0);      while ( Offindex != -1) {        cout<< "offindex :" < <offindex<<endl;        s1.replace (offindex, strlen ("WBM "), " WBM ")  //start with subscript offindex, remove n characters, Replace with the following characters;         offindex  += strlen ("wbm");         offindex = s1.find ("wbm") ,  offindex);    }    cout<< "s1:" <<s1<<endl;     string s3 =  "aaa bbb ccc";     s3.replace ( 0, 3,  "AAA");   //replacement function;    cout<< "s3:" <<s3<<endl;     return 0;}

Operation Result:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/8A/B0/wKiom1g3kG3QGnASAABPDPBlRuo423.png-wh_500x0-wm_3 -wmp_4-s_223576687.png "title=" qq20161125091418.png "alt=" wkiom1g3kg3qgnasaabpdpblruo423.png-wh_50 "/>

(3), deletion and insertion of intervals

The code is as Follows:

#include <iostream> #include <string> #include <algorithm>using namespace std;int  Main (void) {//interval Delete and insert     string s1 =  ' hello1 hello2 hell03 ';     string::iterator it = find (s1.begin (),  s1.end (),  ' l ');     if (it != s1.end ()) {        s1.erase (it);  // Delete algorithm;    }       cout<< "s1 :" <<s1< <endl;    s1.erase (s1.begin (),  s1.end ())  //delete n characters starting from pos;     cout<< "s1 Delete all:" <<s1<<endl;    cout<< "s1 length:" << S1.length () <<endl;    string s2 =  "BBB";     S2.insert (0,  "AAA"),   //head interpolation     s2.insert (s2.length (),  "CCC");//tail Insertion method     cout<<s2<<endl;    return 0;} 

Operation Result:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/8A/B0/wKiom1g3kOWwM1EqAAA6yoij2To820.png-wh_500x0-wm_3 -wmp_4-s_291370346.png "title=" qq20161125091618.png "alt=" wkiom1g3kowwm1eqaaa6yoij2to820.png-wh_50 "/>

(4), case of String conversion----function pointer

The code is as Follows:

#include <iostream> #include <string> #include <algorithm>using namespace Std;int main (void) {string    S1 = "aaabbb";    Transform (s1.begin (), s1.end (), s1.begin (), 0, Toupper);//toupper can be: entry address of function, function object, cout<<s1<<endl;    String s2 = "aaabbb";    Transform (s2.begin (), s2.end (), s2.begin (), 0, tolower);    cout<<s2<<endl; Return 0;}





This article is from the "wait0804" blog, make sure to keep this source http://wait0804.blog.51cto.com/11586096/1876460

String data types in C + +

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.