rfind [C++ Reference]
Keywords: string,rfind
Date: 2010-02-26T06:17:57-0800

## rfind

Syntax:

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

The rfind() function returns the location of the last occurrence of str in the current string, doing a reverse search starting at index:

- string::npos if nothing is found
- starting at zero-based position index counted from the start of the string
- searching at most num characters from the end

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.

         string::size_type 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