C + + String

Source: Internet
Author: User

How to initialize a string object
string S1; S1 empty string
String S2 ("ABC"); Initialize s2 with string literals
String S3 (S2); Initialize S3 to a copy of S2
String S4 (N, ' C ') Initialize S4 to n copies of character ' C '

Common Operations for string
S.empty () Returns true if S is an empty string, and the Buddha returns false
S.size () Returns the number of characters in S
S[n] Returns the position n character in S, starting at 0
S1+s2 Concatenate two strings into a new string, returning the newly generated string
S1=s2 Replace the contents of the S1 with a copy of the S2
V1==v2 The decision is equal, equal returns True, otherwise false is returned
V1!=v2 Determine unequal, not equal to return true, otherwise return false

Questions to note when stitching strings:

string S1 = "Hello";

String S2 ("World");

String s3 = s1 + s2;

string s4 = "Hello" + s2;

string s5 = "Hello" + s2 + "world";

string s6 = "Hello" + "world"; ( This is the wrong way of writing)

The first and second are two ways to initialize a string;

Third is the correct string stitching method;

The last one is the wrong way to stitch a string.

Note: Only string variables of string type and string concatenation within double quotation marks, or two string concatenation of strings is legal, only two double quotation marks caused by string concatenation is not legal.

C + + String

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.