The string class provides strings handling functions that allow you to find characters within a string, extract sequential sequences of characters (called substrings ), and delete and add them in a string. We'll cover some of the main functions.
1.find_first_of () and find_last_of ()
FunctionFind_first_of () andFind_last_of () performs a simple pattern match, such as finding a single character in a stringC. FunctionFind_first_of () Find in string1 occurrences of the character C, while the function find_last_of () finds the last occurrence of c. The return value is the location of the match. If no match occurs, the function returns -1.
int find_first_of (char c, int start = 0):
Finds the 1 occurrences of C in a string , starting with the position start . If there is a match, the matching position is returned; otherwise, 1 is returned . By default,start is 0, and the function searches the entire string.
int find_last_of (char c):
Finds the last occurrence of C in a string . If there is a match, the matching position is returned; 1. The search finds a match at the end of the character, so there is no starting position provided.
Routines:
String str = "Mississippi";
int index;
' s ' appears at index 2,3,5,6
//shih: Here The official return type is String::size_type, which is actually an integer, and the standard library type defines size_type as the unsigned type
index = str.find_first_of (' s ', 0); Index is2
index = str.find_first_of (' s ', 4); Index is5
index = str.find_first_of (' s ', 7); Index is-1, no match
The last appearance of ' s ' in index= 6
index = str.find_last_of (' s ');
The while loop outputs the index of each ' I '
while (index = str.find_first_of (' i ', index))!! =-1)
{
cout << "index" << index << "";
index++; Restart search at Next index
}
Output Result: Index 7 index
2. Extracts sequential character sequences, or substrings, in a string .
The
assumes the location start and the number of characters count.
string substr (int start=0,int count=-1);
Copies the count characters from the starting position and returns these characters as substrings. count character or count is -1, the end of the string stops copying. start, then substr () returns a substring starting from the position to the end of the string. find () function finds the specified pattern in a string. The function will string s and position start as parameters and find s as a substring.
int Find (const string& s,int start = 0):
The search obtains a string Span lang= "en-us" xml:lang= "en-us" >s and location start and find s matches as substrings. If there is a match -1 are returned. By default, start is 0, and the function searches the entire string.
Routines:
String fullname = "Mark Tompkin", FirstName, LastName;
int index;
index = str.find_last_of ("); Index is 4
FirstName = "Mark" LastName = "Tompkin"
FirstName = fullname.sub string (0,index);
LastName = Fullname.substring (index+1);
index = fullname.find ("kin"); Inindex = 9 matches "Kin"
index = Fullname.find ("Omp", 0); match "omp" at index = 6
index = Fullname.find ("Omp", 7); Index is-1 (no match )
C + + string handling functions