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