C string, String object, string literal differences in the text _c language

Source: Internet
Author: User
Tags strcmp strlen

One, string literal value
String literals are a string of constant characters, which are represented by 0 or more characters in double quotes, and all string literals in C + + are automatically added by the compiler at the end of a null character for the compatible C language.
String has no variable name, itself represents itself

Copy Code code as follows:

"Hello world!"//simple string literal
""//empty string literal
"\ncc\toptions\tfile. [cc]\n "//string literal using newlines and tabs

Character literal value: ' A '//single quote:character literal
string literal value: "A"//double quote:character string literal. String containing the letter A and the null character

Concatenation of string literals:

Copy Code code as follows:

Std::out << "a multi-line" +
"String literal" +
"Using concatenation"
<< Std::endl;

Output: A multi-line string literal using concatenation

Multi-line literal value:

Copy Code code as follows:

Std::out << "a multi-line \ n
String literal\n
Using a backslash "
<< Std::endl;

Output: A multi-line string literalusing a backslash

=================================================

1. String literal: Direct quantity of strings:

Copy Code code as follows:

cout<< "Hello" <<endl;

The code outputs it by including the "Hello" string itself, and does not contain a variable of that string.

2. String literals can be assigned to variables, but the memory space associated with the direct amount of the string is in the read-only portion, so it is a constant character array.

Copy Code code as follows:

char* ptr = "Hello";
PTR[1] = ' a ';//crash! Attemps to write to read-only memory.

Therefore, use a character array that points to Const when referring to the direct amount of a string:
Copy Code code as follows:

Const char* ptr = "Hello";
PTR[1] = ' a ';//bug! Attempts to write to read-only memory.

3. When the string is assigned a direct amount to the initial value of the character array. Because the character array is stored in the stack and is not allowed to reference other places of memory, the compiler will copy the string directly into the arrival of the array memory. Therefore, you can make the appropriate modifications.
Copy Code code as follows:

Char stackarray[] = "Hello";
STACKARRAY[1] = ' a ';

Second, C + + style string
C + + style string: When using a C + + style string, consider it as an ordinary type, such as int, which instead avoids having a string as a class to understand many of the problems it poses.

1. Support the same operations performed by many functions in <cstring>.
2. String definition:

Copy Code code as follows:

string myString = "Hello";

3. Operator =: copy string;
Copy Code code as follows:

string newone = original;

The latter is copied to the former, and there is no case where two variables also point to one memory.
4. You can use the operators like int = =
5. You can change one character in a string. Such as
Copy Code code as follows:

string myString = "Hello"; Mystring[0] = ' l ';

This is allowed in the operation.

2.1 C-style string usage
The C + + language manipulates the style string by using a pointer to a (const) char * type.

Copy Code code as follows:

#include <cstring>//CString is a C + + version of the string.h header file, and String.h is a standard library provided by the C language

Standard library functions that manipulate C-style strings (omitted for parameter types, all char * types):
Strlen (s)//returns the length of S, excluding the string terminator null
strcmp (S1, S2)//When S1<S2, returns the value <0, when S1=s2, returns the value =0, when S1>s2, returns the value >0
strcat (S1, S2)//Send string S2 to S1 and return S1
strcpy (S1, S2)//Copy S2 to S1 and return to S1
Strncat (s1, S2, N)//will s2 the first n words fonts back to S1, and return to S1
strncpy (s1, S2, N)//copy S2 's first n characters to S1 and return to S1
if (CP1 < CP2)//compares address, not the values pointed to
const char *CP1 = "A string Example";
const char *CP2 = "A different string";
int i=strcmp (CP1, CP2); I is positive
I=STRCMP (CP2, CP1); I is negative
I=STRCMP (CP1, CP1); I is zero


2.3 Never forget the string terminator null
Copy Code code as follows:

Char ca[]={' C ', ' + ', ' + '}; Not null-terminated
cout << strlen (CA) << Endl; DISASTER:CA isn ' t null-terminated

2.4 The caller must ensure that the target string is of sufficient size
Copy Code code as follows:

Dangerous:what happens if we miscalculate the size of largestr?
Char largestr[16+18+2]; Would hold CP1 a space and CP2
strcpy (Largestr, CP1); Copies CP1 into LARGESTR
strcat (Largestr, ""); Adds a space in end of LARGESTR
strcat (Largestr, CP2); Concatenates CP2 to Largestr
Prints a string example a different string
cout << largestr << Endl;

2.5 using the STRN function to handle C-style strings
Copy Code code as follows:

Char largestr[16+18+2]//To hold CP1 a spaces and CP2
strncpy (Largestr, CP1, 17); Size to copy includes the null
Strncat (Largestr, "", 2); Pedantic, but a good habit
Strncat (Largestr, CP2, 19); Adds at most characters, plus a null

2.6 Use the standard library type string whenever possible
Copy Code code as follows:

string largestr = CP1; Initialize LARGESTR as a copy of CP1
Largestr + = ""; Add space at end of LARGESTR
Largestr + = CP2; Concatenate cp2 onto end of LARGESTR

At this point, the standard library is responsible for managing all internal management issues.

Three, C-style strings
The type of the string literal is essentially an array of const char types. A common structure that C + + inherits from C is a C-style string, and string literals are instances of that type. A C-style string is an array of characters that end with null characters:

Copy Code code as follows:

Char ca1[]={' C ', ' + ', ' + '}; No null, not C-style string
Char ca2[]={' C ', ' + ', ' + ', ' n '}; Explicit null
Char ca3[]= "C + +"; NULL Terminator added automatically
const char *cp= "C + +"; NULL Terminator added automatically
Char *CP1=CA1; Points to the a array, but not C-style string
Char *CP2=CA2; Points to a null-terminated char array

CA1 and CP1 are not C-style strings: CA1 is an array of characters with no terminator null, and the pointer cp1 points to CA1, so it does not point to a null-terminated array.

String Connection:
A string in 1.c++ can replace the char array in C and the former is easier to use
. Connecting two string objects requires only ' + '; C strings are implemented with a char array. The C string is called the char array below

For example:

Copy Code code as follows:

String s1= "Hello", s2= "world";

String s3=s1+s2; can also s3=s1+ "world"

cout<<s3<<endl;//result is HelloWorld


Of course, you can also use + + connection.

2. You can also connect a string object and a char array in this way.

For example:

Copy Code code as follows:

String s1= "Hello";

Char s2[]= "World";

cout<<s1+s2<<endl;//output is HelloWorld


However, you cannot connect two char arrays or character literals in this way.

For example:

Copy Code code as follows:

String s1= "Hello";

String s2= "World";

String s3=s1+ "world";/right, you can connect a string object and a string literal

String s4= "Hello" + "world";//error, cannot connect a string literal

Char s5[]= "World";

String s6=s1+s5;//Correct, you can connect a string object and a char array

Char s7[]= "Hello";

Stirng s8=s7+s5;//Error, you cannot connect two char arrays like this.


In summary, you can only connect two string objects or a string object and a string literal, or one string object and a char array, with + or + =.

Connects a string object and a string literal or char array or returns a String object, so you can concatenate a string object and a string literal (or char array) before connecting to a string literal (or char array).

For example:

Copy Code code as follows:

String s;//initialized to empty
Char s1[]= "Hello";
Char s2[]= "World";
s=s+s1+s2;//Right

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.