How to find some pattern in the String?

find
string s("This_is_an_example_string");
if (s.find("This" != string::npos) { cout<<"Find!!"<<endl;}
else { cout<<"Not found!!"<<endl; }
#npos is a special cases for string (type:size_t), its indicates the pattern is not found in this string.

cout<<"Find the matched pattern from index 5"<<s.find("is", 5)<<endl;
rfind
cout<<"Reverse find the matched pattern position"<<s.rfind("is")<<endl;
find_first_of
cout<<"The first position of found pattern"<<s.find_first_of("is")<<endl;
#The functionality is exactly their name 
find_first_of
 string flag="a";
 int position=0;
 int i=1;
  while((position=s.find_first_of(flag,position))!=string::npos)
 {
   //position=s.find_first_of(flag,position);
   cout<<"position  "<<i<<" : "<<position<<endl;
   position++;
   i++;
 }
#find all the position of the matched pattern
 flag.find_first_not_of
 cout<<"The first position of not matched pattern"<<s.find_first_not_of("is")<<endl; 
 #The functionality is exactly their name  

發表留言