C + + Learning (ix) introductory article--string class

Source: Internet
Author: User

Strings can be stored in string classes instead of character arrays, and string is simpler

If you use the string class, you want to include the header file string in your program, and in the Std namespace, the string class hides the array nature of the strings, and you can handle the strings as you would a normal variable

Program Listing 4.7Strtype1.cpp//Strtypel.cpp-Using the C + + string class#include <iostream>#include<string>intMain () {using namespacestd; Charcharr1[ -]; Charcharr2[ -] ="Jaguar"; stringstr1; stringSTR2 ="Panther"; cout<<"Enter A kind of feline:"; CIN>>charr1; cout<<"Enter Another kind of feline:"; CIN>>str1; Cin.Get(); cout<<"Here is 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:

String object declared as simple variable

The char array is a set of char storage units used to store strings, and the String class is an entity that stores strings

In addition to the array initialization of the available list initialization, 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"};

You cannot assign an array to another array, but you can assign a string object to another string object

Char charr1[]; Char charr2["Jaguar"; string str1; string " Panther " ; charr1=charr2;                      // not allowed to do so str1=str2;                             // can be

The string class simplifies strings merging operations

You can also merge two string objects together

String= str1+str2;str1+=str2;
Program Listing 4.8Strtype2.cpp//Strtype2.cpp--Assigning,adding,and appending#include <iostream>#include<string>intMain () {using namespacestd; stringS1 ="Penguin"; stringS2, 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();}

Results:

Before adding the string class to C + +, you still have to complete the assignment to the string, complete with the function in the header file CString, copy the string to the character array with the function strcpy (), and append the string to the end of the character array with strcat ()

strcpy (CHARR1,CHARR2);    // copy charr2 to charr1strcat (CHARR1,CHARR2);    // Append contents of charr2 to CHARR1
Program Listing 4.9Strtype3.cpp//Strtype3.cpp--More string class features#include <iostream>#include<string>#include<cstring>intMain () {using namespacestd; Charcharr1[ -]; Charcharr2[ -] ="Jaguar"; stringstr1; stringSTR2 ="Panther"; STR1=str2;    strcpy_s (CHARR1,CHARR2); STR1+="Paste"; strcat_s (CHARR1,"Juice"); intLen1 =str1.size (); intLen2 =strlen (CHARR1); cout<<"The string"<< str1 <<"contains"<< len1 <<"characters.\n"; cout<<"The string"<< charr1 <<"contains"<< Len2 <<"characters.\n"; Cin.Get();}

Output:

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

STR1 is an object, and size () is a class object, so calling Str1.size ()

C + + Learning (ix) introductory article--string class

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.