catalogue
Basic concept of string container
string container
Basic concept of string container
String is a C + + style string, and string is essentially a class
The difference between string and char *:
char * is a pointer
String is a class, which encapsulates char * and manages this string. It is a char * container.
Characteristics of string container:
string class encapsulates many member methods
For example: find, copy, delete, replace, insert
string manages the memory allocated by char * without worrying about copy out of bounds and value out of bounds. It is carried out within the class
string constructor
Constructor prototype:
string(); //Create an empty string, for example: string str; string(const char* s); //Initialize with string s string(const string& str); //Initializes another string object with one string object string(int n, char c); //Initialize with n characters c
Case:
#include <string> //string construction void test01() { string s1; //Create an empty string and call the parameterless constructor cout << "str1 = " << s1 << endl; const char* str = "hello world"; string s2(str); //Put c_string converted to string cout << "str2 = " << s2 << endl; string s3(s2); //Call copy constructor cout << "str3 = " << s3 << endl; string s4(10, 'a'); cout << "str3 = " << s3 << endl; }
Assign a value to a string
Function prototype assigned:
string& operator=(const char* s); //char * type string is assigned to the current string string& operator=(const string &s); //Assign the string s to the current string string& operator=(char c); //The character is assigned to the current string string& assign(const char *s); //Assign the string s to the current string string& assign(const char *s, int n); //Assign the first n characters of string s to the current string string& assign(const string &s); //Assign the string s to the current string string& assign(int n, char c); //Assign n characters c to the current string
Case:
//assignment void test01() { string str1; str1 = "hello world"; cout << "str1 = " << str1 << endl; string str2; str2 = str1; cout << "str2 = " << str2 << endl; string str3; str3 = 'a'; cout << "str3 = " << str3 << endl; string str4; str4.assign("hello c++"); cout << "str4 = " << str4 << endl; string str5; str5.assign("hello c++",5); cout << "str5 = " << str5 << endl; string str6; str6.assign(str5); cout << "str6 = " << str6 << endl; string str7; str7.assign(5, 'x'); cout << "str7 = " << str7 << endl; }
String string splicing
Implements splicing strings at the end of a string
Function prototype:
string& operator+=(const char* str); //Overload + = operator string& operator+=(const char c); //Overload + = operator string& operator+=(const string& str); //Overload + = operator string& append(const char *s); //Connects the string s to the end of the current string string& append(const char *s, int n); //Connect the first n characters of string s to the end of the current string string& append(const string &s); //Same as operator + = (const string & STR) string& append(const string &s, int pos, int n); //n characters from pos in string s are connected to the end of the string
Case:
//String splicing void test01() { string str1 = "I"; str1 += "Love playing games"; cout << "str1 = " << str1 << endl; str1 += ':'; cout << "str1 = " << str1 << endl; string str2 = "LOL DNF"; str1 += str2; cout << "str1 = " << str1 << endl; string str3 = "I"; str3.append(" love "); str3.append("game abcde", 4); //str3.append(str2); str3.append(str2, 4, 3); // Starting from the subscript 4 position, intercept 3 characters and splice them to the end of the string cout << "str3 = " << str3 << endl; }
string find and replace
Find: finds whether the specified string exists
Replace: replaces the string at the specified location
Function prototype:
int find(const string& str, int pos = 0) const; //Find the location where str first appears, starting from pos int find(const char* s, int pos = 0) const; //Find the location where s first appears, starting from pos int find(const char* s, int pos, int n) const; //Find the first position of the first n characters of s from the pos position int find(const char c, int pos = 0) const; //Find where the character c first appears int rfind(const string& str, int pos = npos) const; //Find the last position of str, starting from pos int rfind(const char* s, int pos = npos) const; //Find the location of the last occurrence of s, starting from pos int rfind(const char* s, int pos, int n) const; //Find the last position of the first n characters of s from pos int rfind(const char c, int pos = 0) const; //Find where the character c last appeared string& replace(int pos, int n, const string& str); //Replace n characters starting from pos with string str string& replace(int pos, int n,const char* s); //Replace the n characters starting from pos with the string s
Case:
//Find and replace void test01() { //lookup string str1 = "abcdefgde"; int pos = str1.find("de"); if (pos == -1) { cout << "not found" << endl; } else { cout << "pos = " << pos << endl; } pos = str1.rfind("de"); cout << "pos = " << pos << endl; } void test02() { //replace string str1 = "abcdefgde"; str1.replace(1, 3, "1111"); cout << "str1 = " << str1 << endl; }
find is from left to back, and rfind is from right to left
find returns the first character position after finding the string. If it is not found, it returns - 1
When replacing, you need to specify where to start, how many characters to replace and what kind of string to replace
String string comparison
String comparison is based on the ASCII code of characters
=Return 0 > Return 1 < return - 1
Function prototype:
int compare(const string &s) const; //Compare with string s int compare(const char *s) const; //Compare with string s
Case:
//string comparison void test01() { string s1 = "hello"; string s2 = "aello"; int ret = s1.compare(s2); if (ret == 0) { cout << "s1 be equal to s2" << endl; } else if (ret > 0) { cout << "s1 greater than s2" << endl; } else { cout << "s1 less than s2" << endl; } }
String comparison is mainly used to compare whether two strings are equal. It is not of great significance to judge who is big and who is small
String string access
There are two ways to access a single character in a string
char& operator[](int n); //Take characters by [] char& at(int n); //Get characters through at method
Case:
void test01() { string str = "hello world"; for (int i = 0; i < str.size(); i++) { cout << str[i] << " "; } cout << endl; for (int i = 0; i < str.size(); i++) { cout << str.at(i) << " "; } cout << endl; //Character modification str[0] = 'x'; str.at(1) = 'x'; cout << str << endl; }
There are two ways to access a single character in a string, using [] or at
string insert and delete
Insert and delete characters from a string
Function prototype:
string& insert(int pos, const char* s); //Insert string string& insert(int pos, const string& str); //Insert string string& insert(int pos, int n, char c); //Inserts n characters c at the specified position string& erase(int pos, int n = npos); //Delete n characters starting from Pos
Case:
//String insertion and deletion void test01() { string str = "hello"; str.insert(1, "111"); cout << str << endl; str.erase(1, 3); //3 characters from position 1 cout << str << endl; }
String string
Get the desired substring from the string
Function prototype:
string substr(int pos = 0, int n = npos) const; //Returns a string of n characters starting with pos
Case:
//Substring void test01() { string str = "abcdefg"; string subStr = str.substr(1, 3); cout << "subStr = " << subStr << endl; string email = "hello@sina.com"; int pos = email.find("@"); string username = email.substr(0, pos); cout << "username: " << username << endl; }