character string
To use string in c + +, you need to introduce header files
#include<string>
Method for defining string variable
string a;//The string is empty, '' string b="abc";//The string is abc string c=b;//Assign a value to c directly with b, and the content of c is abc string d(5,'c');//The content of d is CCC
Convert to C-style string
Conversion function c_str(), which can convert a string into a C-style string and return the const pointer (const char *) of the string
string path = "D:\\demo.txt"; FILE *fp = fopen(path.c_str(), "rt");
In order to open a file using the fopen() function in the C language, you must convert a string to a C-style string.
String splicing
Use the + or + = operators to splice strings directly
When + is used to splice strings, both sides of the operator can be string strings, a string string and a C-style string, a string string string and a character array, or a string string and a separate character
#include <iostream> #include <string> using namespace std; int main(){ string s1 = "first "; string s2 = "second "; char *s3 = "third "; char s4[] = "fourth "; char ch = '@'; string s5 = s1 + s2; string s6 = s1 + s3; string s7 = s1 + s4; string s8 = s1 + ch; cout<<s5<<endl<<s6<<endl<<s7<<endl<<s8<<endl; return 0; }
//Output results first second first third first fourth first @
Addition, deletion, modification and query of string
1, Insert string
The insert() function can insert another string at the position specified in the string
string& insert (size_t pos, const string& str); //pos indicates the position to insert, that is, the subscript; str represents the string to be inserted. It can be a string or a C-style string. //pos cannot be out of bounds. The range is 0-s.length()
#include <iostream> #include <string> using namespace std; int main(){ string s1, s2, s3; s1 = s2 = "1234567890"; s3 = "aaa"; s1.insert(5, s3); cout<< s1 <<endl; s2.insert(5, "bbb"); cout<< s2 <<endl; return 0; } //s1="12345aaa7890" //s2="12345bbb7890"
2, Delete string
The erase() function deletes a substring in a string
string& erase (size_t pos = 0, size_t len = npos); //The range of pos is 0-s.length() //pos indicates the starting subscript of the substring to be deleted, and Len indicates the length of the substring to be deleted. If len is not specified, all characters from pos to the end of the string are deleted directly (at this time, len = str.length - pos) //If pos is out of bounds, an exception will be thrown; //If len is out of bounds, all characters from pos to the end of the string are extracted.
#include <iostream> #include <string> using namespace std; int main(){ string s1, s2, s3; s1 = s2 = s3 = "1234567890"; s2.erase(5); s3.erase(5, 3); cout<< s1 <<endl; cout<< s2 <<endl; cout<< s3 <<endl; return 0; }
s1=1234567890 s2=12345 s3=1234590
3, Extract substring
The substr() function extracts a substring from a string
string substr (size_t pos = 0, size_t len = npos) const; //pos is the starting subscript of the substring to be extracted, and len is the length of the substring to be extracted. //If pos is out of bounds, an exception will be thrown; //If len is out of bounds, all characters from pos to the end of the string are extracted.
string s = "abcdefg"; string a = s.substr(0, 2); //a="ab"
4, String lookup
The string class provides several functions related to string lookup
1) find() function
The find() function is used to find the position where the substring appears in the string
size_t find (const string& str, size_t pos = 0) const; size_t find (const char* s, size_t pos = 0) const; //The first parameter is the substring to be searched. It can be a string or a C-style string. The second parameter is the starting position (subscript); If not specified, the search starts from the 0th character. //static const size_t npos = -1; That is, the value defined by the constant NPOs is - 1. But because of the type size of NPOs_ T is an unsigned integer type, so NPOs is actually a positive number and is size_ Maximum value of type T.
2) rfind() function
Finds the first occurrence of a character or substring from the back to the front.
string s="abababa"; string a="aba"; s.rfind(a,4);//Return 4
At this time, the reverse search starts from subscript 4. At this time, it matches directly. Note that it returns not 2, but 4,
If the matching is unsuccessful, as shown in the figure below, 2 is returned
3) find_first_of() function
find_ first_ The of() function is used to find the first occurrence of a character shared by a substring and a string in the string
#include <algorithm> size_t find_first_of ( const string& str, size_t pos = 0 ) const; //Find common substrings starting at pos
5, String substitution
//Replace the character len of the specified string starting from pos with str string& replace (size_t pos, size_t len, const string& str); string a="abcd abcd"; a=a.replace(0,a.length(),"aaa"); //Replace the characters at the start and end of the iterator with str string& replace (const_iterator i1, const_iterator i2, const string& str); string a="abcd abcd"; a=a.replace(a.begin(),a.end(),"aaa"); //Replaces the string from the specified position with the specified substring of substr (given starting position and length) string& replace (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen); string a="abcd abcd"; string b="abcdefg"; a=a.replace(0,a.length(),b,1,2);