C++ examples for STL:string
Searching for the last occurrence of a sub string
#include <iostream> #include <string> using namespace std; void showresult(string s, string::size_type i); int main()//from www . j a va 2 s. c o m { string::size_type indx; // Create a string. string str("one two three, test test one two three test test "); string str2; cout << "String to be searched: " << str << "\n\n"; cout << "Searching for the last occurrence of 'two'\n"; indx = str.rfind("two"); showresult(str, indx); return 0; } // Display the results of the search. void showresult(string s, string::size_type i) { if(i == string::npos) { cout << "No match found.\n"; return; } cout << "Match found at index " << i << endl; cout << "Remaining string from point of match: " << s.substr(i) << "\n\n"; }