Reprint to http://www.renfei.org/blog/introduction-to-cpp-string.html operator overload
- + and + =: Connection string
- =: String Assignment
- >, >=, <, and <=: string comparisons (e.g. a < B, AA < AB)
- = =,! =: Compare strings
- <<, >>: output, input string
Note: When using overloaded operator +, you must ensure that at least one of the first two operands is of type string. For example, the following wording is not legal:
#include <iostream><string>int main () { string' cat"; " Apple " " Boy " // illegal! return 0 ;}
Find
stringstr;cin>>Str;str.find ("AB");//returns the position of the string ab in strStr.find ("AB",2);//finds and returns the position of the string AB in str within the str[2]~str[n-1] rangeStr.rfind ("AB",2);//finds and returns the position of the string AB in str within the str[0]~str[2] range//First series FunctionsStr.find_first_of ("Apple");//returns the position of any character in Apple for the first time that it appears in StrStr.find_first_of ("Apple",2);//returns where any character in Apple first appears in the str[2]~str[n-1] rangeStr.find_first_not_of ("Apple");//returns the first occurrence of a character other than Apple in StrStr.find_first_not_of ("Apple",2);//returns the first occurrence of any character except Apple in the str[2]~str[n-1] range//Last series functionsStr.find_last_of ("Apple");//returns the position of any one of the characters in Apple last seen in StrStr.find_last_of ("Apple",2);//returns the position of any of the characters in Apple's last occurrence in the str[0]~str[2] rangeStr.find_last_not_of ("Apple");//returns the last occurrence of any character except Apple in StrStr.find_last_not_of ("Apple",2);//returns the last occurrence of a character other than Apple in the str[0]~str[2] range//If none of the above functions are found, return String::nposcout <<string:: NPOs;
Sub-string
Str.substr (3// return [3] and subsequent substrings str.substr (24// return STR[2]~STR [4-1] substring (that is, a string consisting of 4 characters starting from [2])
Replace
Str.replace (24"sz"); // returns the new string after replacing the contents of [2]~[2+ (4-1)] with "sz" str.replace (24"abcd" 3); // returns a new string that replaces the contents of [2]~[2+ (4-1)] with the first 3 characters of "ABCD"
Insert
Str.insert (2 , " sz ); // Str.insert (2 , abcd ", 3 ); // Str.insert ( 2 , ABCD , 1 , 3 ); //
Additional
In addition to using the overloaded + operator, you can also use a function to do this.
Str.push_back ('a'); // add character ' a 'str.append ("ABC") at the end of STR; // Add the string "ABC" at the end of Str
Delete
Str.erase (3); // Delete [3] and subsequent characters, and return the new string str.erase (35); // Delete the 5 characters starting with [3] and return a new string
Exchange
Str1.swap (STR2); // swap the str1 with the str2
Other
Str.size (); // returns the string length str.length (); // returns the string length str.empty (); // Check if str is empty, return 1 for NULL, otherwise return 0Str[n]; // Access str nth + 1 characters str.at (n); // Access str nth + 1 characters (if overflow throws an exception)
Instance finds the given string and replaces the corresponding substring with another given string
String does not provide such a function, so we implement it ourselves. Because a given string can occur more than once, the find() second parameter of the member function needs to be used, and after each lookup, the search continues from the location where it was found. Look directly at the code (this function returns the number of times the substitution is not replaced if the return value is 0):
intStr_replace (string&STR,Const string&SRC,Const string&dest) { intCounter =0; string:: Size_type pos =0; while(pos = Str.find (src, pos))! =string:: NPOs) {str.replace (pos, Src.size (), dest); ++counter; POS+=dest.size (); } returncounter;}
Removes a given string from the given character
The method is similar to the above and the internal use is erase() complete. Code:
int str_erase (stringconststring src) { int0 ; string 0 ; while string :: NPOs) { str.erase (pos, Src.size ()); + +counter ; } return counter;}
Given a string and a character set, rejecting any character in the character set from a string
int str_wash (stringconststring src) { int0 ; string 0 ; while string :: NPOs) { 1); + +counter ; } return counter;}
C + + string explanation