In C + +, the string is encapsulated into a data type string, which can be declared directly and assigned a string operation such as a value. The following are the differences between string in the C string and C + +:
C string
String Object (c + +)
Required header file name
<string> or <string.h>
<string> or <string.h>
Header file reason required
In order to use String functions
In order to use the String class
Declaration mode
Char name[20];
String name;
Initialization mode
Char name[20]= "Nihao";
String name = "Nihao";
Must the string length be declared?
Is
Whether
Do you use a null character?
Is
Whether
How string assignment is implemented
strcpy (name, "John");
Name = "John";
Advantages
Faster
Easier to use, preferred solutions
Can you assign a string that is longer than the existing character?
No
OK
C + + Common String functions
Char s1[]= "I am a student";
Char s2[20]= "Teacher";
Char s3[]= "student";
int result;
Char s4[20],*p;
(1) string length int strlen (char *str)
Cout<<strlen (S1) <<endl; Output 14
Cout<<strlen (S2) <<endl; Output 7
(2) string copy char *strcpy (char *str1,char *str2)
strcpy (S4,S2); S4 as "teacher"
(3) String connection char *strcat (char *str1,char*str2)
strcat (S2,S3); S2 as "teacherstudent"
(4) string comparison int strcmp (char *str1,char *str) //comparison is the ASCII code value of the corresponding character, if STR1>STR2, returns 1
RESULT=STRCMP (S2,S3); Result>0
RESULT=STRCMP (S2,S2); Result=0
RESULT=STRCMP (S3,S2); Result<0
(5) string positioning char *strchr (char *str,char ch)
P=STRCHR (S1, ' s '); Finds the position of the return character in the string, or returns-1
strcpy (P,S2); S1 is "I am a Teacher"
(6) Find if there is a substring equal to another string in a string
(7) Intercepting substrings to form a new string
Input of strings
(1) method one: Fill a C string variable with an input operator
For example:
Char a[80];
cin>>a;
Note: When reading a C string in this manner, the original whitespace characters (spaces, tabs, and line breaks) are ignored, and the input stops at the next space or line break.
(2) method two: use predefined function getline to get whole line input (including spaces)
The Getline function has two parameters: the first parameter is used to receive the input C string variable, and the second parameter is used to specify the maximum number of characters the getline can receive.
For example:
Char a[80];
Cin.getline (a,80);
When the line ends, the input stops.
Input of C + + string class
(1) method one: and C string Input method is the same.
(2) Method Two: Use the Getline function.
For example:
String A;
Getline (Cin,a);
Conversion between string objects and C strings
You can store a C string in a variable of type string, for example:
Char a[] = "Nihao";
String B;
B=a;
However, a string object cannot be automatically converted to a C string, requires an explicit type conversion, and needs to use the member function c_str () of the String class.
For example:
strcpy (A,b.c_str ());
Conversion of strings to numbers
The Atoi function gets a c string parameter that returns the corresponding int value. If the argument does not correspond to an int value, the Atoi returns 0. The Atoi function is in the library where the file is cstdlib. If the number is too large to be converted to a value of type int, you can use ATOL to convert the string to a long type value.
For example:
atoi ("1234"); //return integer 1234
atoi ("#123"); //return 0