C ++ Learning (9) Quick Start: String class, Quick Start: string

Source: Internet
Author: User
Tags control characters

C ++ Learning (9) Quick Start: String class, Quick Start: string

You can use the string class instead of character array to store strings. string is simpler.

If you use the string class, you need to include the header file string in the program, which is located in the std namespace. The string class hides the array nature of the string and can process the string like a common variable.

Program list 4.7 strtype1.cpp // strtypel. cpp--using the C ++ string class # include <iostream> # include <string> int main () {using namespace std; char charr1 [20]; char charr2 [20] = "jaguar"; string str1; string str2 = "panther"; cout <"Enter a kind of feline:"; cin> charr1; cout <"Enter another kind of feline:"; cin> str1; cin. get (); cout <"Here are some felines: \ n "; cout <charr1 <"" <charr2 <"" <str1 <"" <str2 <endl; cout <"The third letter in" <charr2 <"is" <charr2 [2] <endl; cout <"The third letter in" <str2 <"is" <str2 [2] <endl; cin. get ();}

Output result:

The string object is declared as a simple variable.

The char array is a group of char storage units used to store strings. The string class is an entity that stores strings.

In addition to array initialization available list initialization, it also applies to string Initialization

Char first_date [] = {"Le Chapon Dodu "};

String second_date = {"Le Chapon Dodu "};

Char third_date [] {"Le Chapon Dodu "};

String forth_date {"Le Chapon Dodu "};

 

An array cannot be assigned to another array, but a string object can be assigned to another string object.

Char charr1 [20]; char charr2 [20] = "jaguar"; string str1; string str2 = "panther"; charr1 = charr2; // str1 = str2 is not allowed; // Yes

String class simplifies string Merging

You can also combine two string objects.

string str3;str3 = str1+str2;str1+=str2;
Program list 4.8 strtype2.cpp // strtype2.cpp--assigning, adding, and appending # include <iostream> # include <string> int main () {using namespace std; string s1 = "penguin"; string s2, s3; cout <"You can assign one string object to another: s2 = s1 \ n"; s2 = s1; cout <"s1:" <s1 <", s2:" <s2 <endl; cout <"You can assign a C-style string to a string object. \ n "; cout <" s2 = \ "buzzard \" \ n "; s2 =" buzzard "; cout <" s2: "<s2 <endl; cout <"You can concatenate strings: s3 = s2 + s1 \ n"; s3 = s2 + s1; cout <"s3:" <s3 <endl; cout <"You can append strings. \ n "; s1 + = s2; cout <" s1 + = s2 yields s1 = "<s1 <endl; s2 + =" for a day "; cout <"s2 + = \" for a day \ "yields s2 =" <s2 <endl; cin. get ();}

Result:

Before adding a string class to C ++, you still need to assign a value to the string, use the function in the header file cstring, use the strcpy () function to copy the string to the character array, and use strcat () append a string to the end of the character array

strcpy(charr1,charr2);    //copy charr2 to charr1strcat(charr1,charr2);    //append contents of charr2 to charr1
Program list 4.9 strtype3.cpp // strtype3.cpp--more string class features # include <iostream> # include <string> # include <cstring> int main () {using namespace std; char charr1 [20]; char charr2 [20] = "jaguar"; string str1; string str2 = "panther"; str1 = str2; strcpy_s (charr1, charr2 ); str1 + = "paste"; strcat_s (charr1, "juice"); int len1 = str1.size (); int len2 = strlen (charr1 ); cout <"The string" <str1 <"contains" <len1 <"characters. \ n "; cout <" The string "<charr1 <" contains "<len2 <" characters. \ n "; cin. get ();}

Output:

The strlen () function is a regular function that returns the number of characters contained in the string.

Str1 is an object, and size () is a Class Object. Therefore, str1.size () is called in this way ()

 

String class I/O

//strtype4.cpp - - line input#include<iostream>#include<string>#include<cstring>int main(){    using namespace std;    char charr[20];    string str;    cout << "Length of string in charr before input:"        << strlen(charr) << endl;    cout << "Length of string in str before input:"        << str.size() << endl;    cout << "Enter a line of text:\n";    cin.getline(charr, 20);    cout << "You entered: " << charr << endl;    cout << "Enter another line of text:\n";    getline(cin, str);    cout << "You entered:" << str << endl;    cout << "Length of string in charr after input:"        << strlen(charr) << endl;    cout << "Length of string in str after input:"        << str.size() << endl;    cin.get();}

Output:

Array charr string length is 31

Indicates the uninitialized data. The first null character is displayed at random. It is only a few bytes after the end of the array.

Getline (cin, str );

Use cin as a parameter to specify where to search for the input, nor to specify the length of the string, because the string object automatically adjusts its size based on the length of the string.

Getline () is an istream class method, but a long time before the string class was introduced, C ++ had an istream class, so there was no class method to process the string object.

 

Other string literal values

wchar_t title[ ] =L"Chief Astrogator";char16_t name[ ] =u"Felonia Ripova";char32_t car[ ] =U"Humber Super Snipe";

C ++ uses the prefix L, u, and U respectively.

 

You can also use the original string type. In the original string, the character represents your own

cout<<R"(Jim "King" Tutt uses  "\n" instead of endl.)"<<'\n';

Here \ n does not represent a line break, but a regular character slash and n

"()" Is used as the delimiter and the prefix R is used to identify the original string.

When you enter the original string, press enter to not move to the next line, but add the carriage return character to the original string.

 

If you want to include in the original string) ", because the compiler sees the first)" will end here, so you need to redefine the boundary symbol.

You can add any other characters (excluding the left brackets, right brackets, spaces, slashes, and control characters) to the "and" at the beginning) ''to add the same other characters

cout<<R"+*("(Who wouldn't?)",she whispered.)+*"<<endl;

You can combine R with other string prefixes, such as Ru or UR, to identify strings such as wchar_t.

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.