rfind [C++ Reference]
Keywords: string,rfind
Date: 2009-10-08T22:07:33-0700

[[rfind]]
C++ Reference
Translations of this page?:

- en
- br-pt
- cn
- fr
- it
- jp
- pl
- ru
- tr

## rfind

Syntax:

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

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

Edit this page • Old revisions
• • • Login • Index • Recent changes • RSS • cc •