C + + string

Source: Internet
Author: User
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)!

  • Related Article

    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.