string:rfind

C++ Reference

rfind

Syntax:

    #include <string>
    size_type rfind( const string& str, size_type index );
    size_type rfind( const char* str, size_type index );
    size_type rfind( const char* str, size_type index, size_type num );
    size_type rfind( char ch, size_type index );

The rfind() function either:

* returns the location of the first occurrence of str in the current string,

  doing a reverse search from index, string::npos if nothing is found,
 
  doing a reverse search from index, searching at most num characters, string::
  npos if nothing is found,
 
  doing a reverse search from index, string::npos if nothing is found.

For example, in the following code, the first call to rfind() returns string:: npos, because the target word is not within the first 8 characters of the string. However, the second call returns 9, because the target word is within 20 characters of the beginning of the string.

     int loc;
     string s = "My cat's breath smells like cat food.";
     loc = s.rfind( "breath", 8 );
     cout << "The word breath is at index " << loc << endl;
     loc = s.rfind( "breath", 20 );
     cout << "The word breath is at index " << loc << endl;

Related Topics: find, find_first_not_of, find_first_of, find_last_not_of, find_last_of