When we start to use the C language to deal with strings, it will feel very troublesome. C language is missing the corresponding string processing functions, if you want to implement a string function, only on our own to implement. But when it comes to C + +, the processing of strings becomes very simple. Today we are going to learn the most high-frequency string processing functions in C + +. Sample code uploaded to: https://github.com/chenyufeng1991/CppString.
First you introduce a string header file in C + +:
#include <string>
Please note that the header file here is not. h, otherwise it becomes the header file in C language.
(1) Create a string
There are several ways to create a string, and the most typical way is to use a copy constructor,
String str ("Chenyufeng", 3);
cout << str << Endl;
Copies the most recent string of 3 characters at the beginning of the original string. Print results for Che.
String str2 ("Chenyufeng", 2,3);
cout << str2 << Endl;
Copies the most recent 3-character string at the beginning of the original string index=2. The print result is eny.
=: string Assignment
str2 = "Robert";
cout << str2 << Endl;
You can also assign a value string to a variable in a direct way, using "=". The result of the print is Robert.
(2) Swap: Swap the value of two strings
Swap: Swap two string values string
string1 = "Chen";
String string2 = "Yufeng";
Swap (string1, string2);
cout << "string1 = << string1 <<"; string2 = "<< string2 << Endl;
The print result is already exchanged with the value of the original string.
(3) +,append: Adding strings
+ =, append: Add string at tail
stringorigin = "Chen";
String stringappend = "Yufeng";
Stringorigin = Stringorigin + stringappend;
cout << "Stringorigin =" << stringorigin << Endl;
Stringorigin.append ("_ok");
cout << "stringoriginappend =" << stringorigin << Endl;
Note that adding a string operation modifies the original string. It is convenient to add the string directly using the + number.
(4) Insert: Inserts a string at the specified position
Insert: Inserts a string
Stringinsertorigin = "Chenyufeng" at the specified position;
Stringinsertorigin.insert (3, "__");
cout << "Stringinsertorigin =" << stringinsertorigin << Endl;
The above code can insert a __ underline at the indx=3 position and print the result to Che__nyufeng.
(5) Erase,clear Delete string
Erase: Delete character
string stringeraseorigin = "Chenyufeng";
Stringeraseorigin.erase ();
cout << "Stringeraseorigin =" << stringeraseorigin << Endl;
Clear: Delete all characters
string stringclearorigin = "Chenyufeng";
Stringclearorigin.clear ();
cout << "Stringclearorigin =" << stringclearorigin << Endl;
The above operation is actually to empty the string.
(6) Replace: replacement string
Replace: Replaces the string, the size character at the beginning of a POS position to replace with the following "" string
stringreplaceorigin = "Chenyufeng";
Stringreplaceorigin.replace (3, 2, "the");
cout << "Stringreplaceorigin =" << stringreplaceorigin << Endl;
The above code replaces the string 2 characters starting with index=3 with "66" and prints the result as Che66ufeng.
(7) ==,<, <=, >=: Comparing string size
The use of this operator in C + + to manipulate strings is in fact an operator overload. String comparisons are sized according to the dictionary order of the letters or ASCII values. Until you compare the different letters of two strings or compare to the last stop of a string.
==,<,>,<=,>=: Comparing strings string
stringleft = "Zhen";
String stringright = "Yufeng";
if (Stringleft = = stringright)
{
cout << "equal" << Endl;
}
if (stringleft!= stringright)
{
cout << "Not Equal" << Endl;
}
if (Stringleft < stringright)
{
cout << "Stringleft < stringright" << Endl;
}
if (Stringleft > Stringright)
{
cout << "Stringleft > Stringright" << endl;
}
(8) Size,length: Calculate string length
The computed string length here differs from the C language in that it does not include the end of the, and calculates the true length.
Size (), Length (): evaluates string length string
stringcount = "Chenyufeng";
cout << "stringsize =" << stringcount.size () << Endl;
cout << "stringlength =" << stringcount.length () << Endl;
The above print results are 10.
(9) Empty: Determine if the string is empty
Empty (): Determines whether the string is empty, string
stringisempty = "";
String stringnotempty = "Chen";
if (Stringisempty.empty ())
{
cout << "stringisempty = = Empty" << Endl;
}
else
{
cout << "stringisempty!= empty" << Endl;
}
if (Stringnotempty.empty ())
{
cout << "stringnotempty = = Empty" << Endl;
}
else
{
cout << "stringnotempty!= empty" << Endl;
}
(10) input and output stream of string
Input output stream
cout << "Please enter a string" <<endl;
string Stringinput;
Cin >> Stringinput;
cout << "stringinput =" << stringinput << Endl;
A string can also use an input-output stream similar to other C + + data types. You can use the ENTER key to end the input stream.
(one) Max_size: The maximum number of strings to hold.
Max_size:
string stringmaxsize;
cout << "stringmaxsize =" << stringmaxsize.max_size () << Endl;
Print results: 18446744073709551599. Indicates that the string can hold so many characters.
(a) [], at: element access and modification
[],at (): element access
string Stringat = "Chenyufeng";
cout << "stringat[3] =" <<stringat[3] << Endl;
cout << "stringat.at (3) =" << stringat.at (3) << Endl;
STRINGAT[3] = ' 6 ';
stringat.at (5) = ' 9 ';
cout << "Stringat =" << stringat << Endl;
Strings can be manipulated as arrays, accessed using subscripts, and can be modified with the original string.
(Compare): Comparison of strings, returning 0,1,-1.
Compare ()
string stringcompare = "Chenyufeng";
int AAA = Stringcompare.compare ("Chen"); > 0
int bbb = Stringcompare.compare ("Chenyufeng");//= 0
int CCC = Stringcompare.compare ("Done");//< 0
cout << "AAA = << aaa <<"; bbb = "<< bbb <<"; CCC = "<< CCC << Endl;"
(SUBSTR): Fetch substring
substr
String stringsubstr = "Chenyufeng";
3 characters starting with index 4
cout << "Stringsubstr.substr (4,3) =" << stringsubstr.substr (4,3) << Endl;
All characters starting with index 4
cout << "Stringsubstr.substr (4) =" <<stringsubstr.substr (4) << Endl;
Entire character
cout << "stringsubstr.substr () =" <<stringsubstr.substr () << Endl;
Find: Finding a character
Find
string stringfind = "Chenyufeng";
Stringfind.find (' n ');
cout << "Stringfind.find (' n ') =" << stringfind.find (' n ') << Endl;
cout << "stringfind.find_first_of (' e ') =" << stringfind.find_first_of (' e ') << Endl;
cout << "stringfind.find_last_of (' e ') =" << stringfind.find_last_of (' e ') << Endl;
The default find function is the subscript index that returns the first occurrence of a character. Find_first_of and find_last_of are respectively the first and last occurrences of a character's index.
The string processing functions in these 15 C + + are the most common, of course there are many others, I will continue to add in the subsequent use. String is actually part of the STL. About the use of String functions more specifically for official documents: http://en.cppreference.com/w/cpp/string/basic_string.