C string string object the difference between string literals

  • 2020-04-02 01:31:53
  • OfStack

The literal value of a string
A string literal is a string of constant characters that are represented by zero or more characters enclosed in double quotes. For compatibility with C, all string literals in C++ are automatically appended by the compiler with a null character at the end.
The string has no variable name and represents itself


"Hello World!" //simple string literal
"" //empty string literal
"nCCtoptionstfile.[cC]n" //string literal using newlines and tabs

Character literal: 'A' //single quote:character literal
Character string literal: "A" //double quote:character string literal

Concatenation of string literals:


std::out << "a multi-line "+
"string literal"+
" using concatenation"
<< std::endl;

Output: a multi-line string literal using concatenation

Multi-line literal:


std::out << "a multi-line n
string literaln
using a backslash"
<< std::endl;

Output: a multi-line string literalusing a backslash

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

1. String literal:


cout<<"hello"<<endl;

The code outputs the "hello" string by including the string itself, not the string's variable.

2. The direct value of a string can be assigned to a variable, but the memory space associated with the direct value of a string is in the read-only part, so it is an array of constant characters.


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

Therefore, use an array of characters pointing to const when referring to a direct value of a string:

const char* ptr = "hello";
ptr[1] = 'a';//bug! attempts to write to read-only memory.

3. When assigning a string directly to the initial value of a character array. Since character arrays are stored in the stack and are not allowed to reference memory elsewhere, the compiler copies the string directly into the array memory at the station. Therefore, changes can be made accordingly.

char stackArray[] = "hello";
stackArray[1] = 'a';

C++ style string
C++ style strings: when using C++ style strings, treat them as a normal type, such as int, which will avoid many of the problems of understanding string as a class.

1. Support < cstrings > Many of these functions do the same thing.
2. String definition:


string myString =  " hello "; 

3. Operator = : copy string; Such as:

string newone = original;

The latter is copied to the former, so that two variables do not point to the same memory.
4. You can use operators like == like int
5. You can change a character in a string. Such as

string myString = "hello"; mystring[0] = 'l'; 

This operation is allowed.

2.1 use of c-style strings
The C++ language manipulates c-style strings with Pointers of type (const) char *.


#include <cstring> //Cstring is the C++ version of the string.h header file, and string.h is the standard library provided by the C language
//Manipulation of standard library functions for c-style strings (parameter type omitted, all char * type) :
strlen(s) //Returns the length of s, excluding the string terminator null
strcmp(s1, s2) //When s1 <S2, return value <0, when s1=s2, return value =0, when s1> When s2, return value> 0
strcat(s1, s2) //Connect the string s2 to s1 and return s1
strcpy(s1, s2) //Copy s2 to s1 and return s1
strncat(s1, s2, n) //Concatenate the first n characters of s2 to the end of s1 and return s1
strncpy(s1, s2, n) //Copy the first n characters of s2 to s1 and return 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

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

// Dangerous:What happens if we miscalculate the size of largeStr?
char largeStr[16+18+2]; // will hold cp1 a space and cp2
strcpy(largeStr, cp1); // copies cp1 into largeStr
strcat(largeStr, " "); // adds a space at end of largeStr
strcat(largeStr, cp2); // concatenates cp2 to largeStr
// prints A string example A different string
cout << largeStr << endl;

2.5 use the STRN function to handle c-style strings

char largeStr[16+18+2] // to hold cp1 a space 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 18 characters, plus a null

2.6 use the standard library type string whenever possible

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 standards library is responsible for handling all internal management issues.

C style string
The type of the string literal is essentially an array of type const char. A common structure C++ inherits from the C language is the c-style string, and the string literal is an instance of that type. C-style string is an array of characters ending in null:


char ca1[]={'C', '+', '+'}; // no null, not C-style string
char ca2[]={'C', '+', '+', '0'}; // explicit null
char ca3[]="C++"; // null terminator added automatically
const char *cp="C++"; // null terminator added automatically
char *cp1=ca1; // points to first element of a array, but not C-style string
char *cp2=ca2; // points to first element of a null-terminated char array

Neither ca1 nor cp1 is a c-style string: ca1 is an array of characters with no ending null, and the pointer cp1 points to ca1, so it does not point to a null-terminated array.

String concatenation:
1. String in c++ can replace char array in c and the former is more convenient to use
. To join two string objects, just use '+'; C strings are implemented using a char array. The following c strings are called char arrays

Such as:


string s1="hello",s2="world";
          string s3=s1+s2;                  //Can also be s3 = s1 + "world"
cout<<s3<<endl;//The results for the helloworld

And then of course you can connect it with +=.

2. You can also concatenate a string object with a char array like this.

Such as:


string s1="hello";
          char s2[]="world";
cout<<s1+s2<<endl;//The output is helloworld

You cannot concatenate two char arrays or character literals in this way.

Such as:


string s1="hello";
           string s2="world";
           string s3=s1+"world";//Yes, you can connect a string object to a string literal
           string s4="hello"+"world";//Error. Cannot concatenate a string literal like this
           char s5[]="world";
           string s6=s1+s5;//Yes, you can concatenate a string object with a char array
           char s7[]="hello";
           stirng s8=s7+s5;//Error, cannot concatenate two char arrays like this.

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

Concatenate a string object to a string literal or char array or return a string object, so concatenate a string object to a string literal (or char array) and then concatenate a string literal (or char array).

Such as:


string s;//Initialize to null
char s1[]="hello";
char s2[]="world";
s=s+s1+s2;//correct


Related articles: