In writing C + + programs, there is always a case where you want to look up a small string from a string, and for C, we often use either strstr () or STRCHR (). and for C + + string, we tend to use find ().
C + +: #inlcude <string>
C: #include <string.h>
Find (): Finds a single specified character or character array in a string. If found, returns the start position of the first match, and returns String::npos if no matching content is found.
Find_first_of (): Finds in a target string that is the first character position to match any character in the specified set of characters. Returns NPOs if no matching content is found.
Find_last_of (): Finds in a target string, returns the last character position that matches any character in the specified set of characters. Returns NPOs if no matching content is found.
Find_first_not_of (): Finds in a target string, returning the first element position that does not match any of the characters in the specified character group. Returns NPOs if no such element is found.
Find_last_not_of (): Finds in a target string that returns the position of the largest element that does not match any character in the specified set of characters. If no such element is found, return to NPOs.
RFind (): Finds a single specified character or group of characters for a string from the end to the header. If found, returns the starting position of the first match, or NPOs if no match is found.
Find (string, int): The first parameter is used to indicate the character to look for, and the second argument is used to indicate where to start looking for substrings from the string (the default lookup location is 0).
Example: string matching:
| code is as follows |
copy code |
#include "stdafx.h" #include <iostream> #include <math.h> #include <string> using namespace Std; int _tmain (int argc, _tchar* argv[]) { String t;//Original string String p;//mode while (CIN>>T>>P) { int count=0; int begin=-1; while ((Begin=t.find (p,begin+1))!=string::npos) { count++; } cout<<count<<endl; } int z; cin>>z; return 0; } The results of the operation are:
The string to search is ' Heartbeat ' Element in ' ABCDE ' found at position 1 Element in ' ABCDE ' found at position 2 Element in ' aeiou ' found at position 6 Element in ' aeiou ' found at position 1 ' E ' found at position 6 |
Example
| code is as follows |
copy code |
| #include <iostream> #include <string> using namespace std; Int main () { string s = "**gteate wall**!"; string t = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"; cout<< "s:" <<s<<endl; cout<< t: "<<T<<ENDL; int first=s.find_first_of (t); if (string::npos) { cout<< "s all characters are not in T" <<endl }else { cout<< the first character of the character appearing in T in S: "<<s[first]<<endl; } int last = s.find_last_of (t); if (last = = String::npos) { cout<< "s all characters are not in T" <<endl return 1 ; }else { cout<< "s The last character of the character appearing in T:" <<s[last]<<endl return 1; } } |
Find
String str;
CIN >> str;
Str.find ("AB");/return string AB position in str
Str.find ("AB", 2);//find within Str[2]~str[n-1] range and return the string ab position in str
Str.rfind ("AB", 2);//find within STR[0]~STR[2] range and return the string ab position in str
The function of the series
Str.find_first_of ("Apple")//returns the location of the first occurrence of any character in the Apple in Str
Str.find_first_of ("Apple", 2)//returns the position where any of the characters in Apple first appear in the str[2]~str[n-1] range
Str.find_first_not_of ("Apple")//returns the location of the first occurrence of any character other than Apple in Str
Str.find_first_not_of ("Apple", 2)//Returns the location of the first occurrence of any character except Apple in the str[2]~str[n-1] range
Last series functions
Str.find_last_of ("Apple")//returns the last occurrence of any character in the Apple in Str
Str.find_last_of ("Apple", 2)//Returns the location of the last occurrence of any character in Apple in the str[0]~str[2] range
Str.find_last_not_of ("Apple")//returns the last occurrence of any character other than Apple in Str
Str.find_last_not_of ("Apple", 2)//Returns the last occurrence of any character except Apple in the str[0]~str[2] range
//The above function, if not found, returns String::npos
cout << string::npos;