public member function
<string>

std::string::rfind

string (1)
size_t rfind (const string& str, size_t pos = npos) const;
c-string (2)
size_t rfind (const char* s, size_t pos = npos) const;
buffer (3)
size_t rfind (const char* s, size_t pos, size_t n) const;
character (4)
size_t rfind (char c, size_t pos = npos) const;
string (1)
size_t rfind (const string& str, size_t pos = npos) const noexcept;
c-string (2)
size_t rfind (const char* s, size_t pos = npos) const;
buffer (3)
size_t rfind (const char* s, size_t pos, size_t n) const;
character (4)
size_t rfind (char c, size_t pos = npos) const noexcept;
Find last occurrence of content in string
Searches the string for the last occurrence of the sequence specified by its arguments.

When pos is specified, the search only includes sequences of characters that begin at or before position pos, ignoring any possible match beginning after pos.

Parameters

str
Another string with the subject to search for.
pos
Position of the last character in the string to be considered as the beginning of a match.
Any value greater or equal than the string length (including string::npos) means that the entire string is searched.
Note: The first character is denoted by a value of 0 (not 1).
s
Pointer to an array of characters.
If argument n is specified (3), the sequence to match are the first n characters in the array.
Otherwise (2), a null-terminated sequence is expected: the length of the sequence to match is determined by the first occurrence of a null character.
n
Length of sequence of characters to match.
c
Individual character to be searched for.

size_t is an unsigned integral type.

Return Value

The position of the first character of the last match.
If no matches were found, the function returns string::npos.

size_t is an unsigned integral type.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// string::rfind
#include <iostream>
#include <string>

int main ()
{
  std::string str ("The sixth sick sheik's sixth sheep's sick.");
  std::string key ("sixth");

  unsigned found = str.rfind(key);
  if (found!=std::string::npos)
    str.replace (found,key.length(),"seventh");

  std::cout << str << '\n';

  return 0;
}


The sixth sick sheik's seventh sheep's sick.

Complexity

Unspecified, but generally up to linear in the string length (or pos) times the number of characters to match (worst case).

Iterator validity

No changes.

Data races

The object is accessed.

Exception safety

If s does not point to an array long enough, it causes undefined behavior.
Otherwise, the function never throws exceptions (no-throw guarantee).

See also