Common function Descriptions of string in C + +

Source: Internet
Author: User

String can be said to be an upgraded version of the character array, the use of more convenient, not prone to error. In this paper, the common function of string is briefly introduced, so it can be used.

Common functions in string are divided into four classes, assignment, addition, comparison, and deletion.

First, assign a value

1.str= "ADF"

2.str.assign ("ADF"); Str.assign (str1);//assigns the string in parentheses to STR, str.assign (str1,1,4);//assigns str1 1-4 to STRs; Str.assign (5, ' a ' );//Assign five A to str; Str.assign ("AFF", 4);//Assign "aff/0" to Str

3.

Second, add

1.str.push_back (' a '); Add the last character a to STR, and note that only a single character can be added.

2.str.insert (2, "ADF"); Insert function to insert ADF into the second character of Str specifier

3.str+= "ADF"//plus an ADF string

4.str.sppend ("ADF");//Add a string at the end;

5.str.sppend (str1,1,3);//Multibyte the str1 1-3-bit word behind STR;

6.str.append ("ABCD", 5); Str.append (5, ' X '); ibid., just in the tail insert
 

Third, Comparison

The 1.C + + string supports common comparison operators (>,>=,<,<=,==,!=) and even supports string-to-c-string comparisons (such as str< "Hello"). When using the >,>=,<,<= operators, the characters are compared in dictionary order according to the current character attribute. Dictionaries are smaller than the previous characters, and the order of comparison is compared from front to back, and the size of the two strings is determined by the comparison of the two characters in this position by encountering unequal characters. Also, string ("AAAA") <string (AAAAA).

2.compare function Comparison

String str ("ABCD");
Str.compare ("ABCD"); STR and "ABCD" compare, same return 0
Str.compare ("DCBA"); Returns a value that is less than 0
Str.compare ("AB"); Returns a value greater than 0
Str.compare (s); Equal, returns 0
Str.compare (0,2,str,2,2); With Str starting from 0 two bits and str from 2 Two-bit comparison, returns the number less than 0
Str.compare ("Bcx", 2); With "BC" and "BC" comparison, return 0

Iv. deletion

1. Empty

Str.clear (); Str.erase ();

2. Remove from specified location

Str.erase (13);//delete all from 13-bit onwards
Str.erase (7,5);//delete 5 from 7 bits

There's a replacement function.

Str.replace ("asdf");//Replace the 1-bit starting 2 with a asdf

Common function Descriptions of string 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.