Searching for the last occurrence of a substring in a string : string find « string « C++ Tutorial






#include <iostream>
#include <string>

using namespace std;
void showresult(string s, string::size_type i);

int main()
{
  string::size_type indx;
  string str("one two three, one two three");
  string str2;

  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";
}








15.19.string find
15.19.1.search a sub string
15.19.2.string.find(substring)
15.19.3.string.rfind(substring)
15.19.4.string.find_first_of( substring )
15.19.5.Find the first occurance
15.19.6.Find first out of
15.19.7.string.find_last_of(substring)
15.19.8.string.find_first_not_of( substring )
15.19.9.Find the first that's not in this set, starting from the end
15.19.10.Find the first of any of these chars starting from the end
15.19.11.Search from the beginning
15.19.12.Search from the end
15.19.13.Find the first of any of these chars
15.19.14.Find the first that's not in this set
15.19.15.find last occurrence of a character - equivalent of strrchr()
15.19.16.find first occurrence of a substring - equivalent of strstr()
15.19.17.find last occurrence of a substring - no C-string equivalent
15.19.18.find first occurrence of a character - equivalent of strchr()
15.19.19.Searching for the first occurrence of a substring in a string
15.19.20.Searching for the last occurrence of a substring in a string
15.19.21.Searching for the first occurrence of a letter in a string
15.19.22.Searching for the last occurrence of a letter in a string
15.19.23.Searching for the first occurrence of any character other than o, n, e, or space
15.19.24.Searching for the last occurrence of any character other than o, n, e, or space
15.19.25.Using find with reverse iteration