17. C ++-string class (detailed description), 17. c-string

Source: Internet
Author: User

17. C ++-string class (detailed description), 17. c-string

C ++ string class

In the C language, strings are represented by character arrays. For the application layer, strings are often used. Continuing to use character arrays reduces the efficiency.

Therefore, in the C ++ standard libraryClass stringNew custom string.

Header file:# Include <string>

  • StringString connection
  • StringString size comparison
  • StringSubstring search and Extraction
  • String directly supports stringInsert and replace

Common constructors of the string class include:

String str; // generate an empty string str ("ABC") // equivalent to str = "ABC"
String str ("ABC", strlen) // save "ABC" to str. A maximum of the first strlen bytes of string s ("ABC", stridx, strlen) can be stored) // store the stridx position of "ABC" as a string starting with str. A maximum of strlen bytes can be stored. string s (strlen, 'A') // store strlen 'A' to str

Common member functions of the string class include:

Str1.length (); // obtain str1.swap (str2); // replace str1.puch _ back ('A') of str1 and str2 '); // Add A 'A' character at the end of str1. The parameter must be in the character form str1.append ("ABC"); // Add A "ABC" string at the end of str1, the parameter must be in the string format str1.insert ("ABC", 2); // Insert "ABC" str1.erase (2) at the subscript of str1 2 ); // Delete the position where the subscript is 2, for example, "ABCD" --> "AB" str1.erase (); // delete one from the position where the subscript is 2, for example: "ABCD" --> "ABD" str1.clear (); // Delete All str1.replace (2, 4, "ABCD"); // Replace four bytes from the position where the subscript is 2, is "ABCD" str1.empty (); // if it is null, true is returned.

Reverse correlation (In header file <algorithm>)

String str ("hello"); reverse (str. begin (), str. end (); cout <str <endl; // reverse

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.