C++ std::string rfind() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::string

Definition

In C++, std::string::rfind() finds the last place where a substring or character appears.

If there is no match, it returns std::string::npos. Include <string>.

This is a member of std::string. A string grows as you append, stores characters contiguously, and can reallocate—after growth, old iterators and references may be invalid. Prefer at() when you want bounds checks; remember find returns npos when missing.

Syntax

Call like find, but you get the rightmost match.

C++
auto p = s.rfind("na");
auto q = s.rfind('a', 3);
CallWhat it does
s.rfind(str)Last start index of str, or npos.
s.rfind(str, pos)Search as if the string ended at pos (inclusive limit).

The optional pos is the highest index where a match may start—not a reverse offset.

Library declarations
C++
size_t rfind(const string& str, size_t pos = npos) const;
size_t rfind(char ch, size_t pos = npos) const;

Parameters

ParameterDescription
str / chText or character to find.
posOptional upper bound for the start index of a match.

Return Value

Return ValueDescription
IndexStart position of the last match.
nposWhen not found.

C++ Examples

Example 1 — Basic rfind()

Find the last "na" in banana.

C++
#include <iostream>
#include <string>

int main() {
    std::string s = "banana";
    std::size_t p = s.rfind("na");
    std::cout << p << '\\n';
    return 0;
}

How It Works

  1. "banana" contains "na" at 2 and 4.
  2. rfind returns the last start index, 4.
  3. find would have returned 2.

Example 2 — rfind a Character

Find the last slash in a path.

C++
#include <iostream>
#include <string>

int main() {
    std::string path = "/a/b/c.txt";
    std::size_t p = path.rfind('/');
    std::cout << p << '\\n';
    std::cout << path.substr(p + 1) << '\\n';
    return 0;
}

How It Works

  1. The last '/' is at index 5.
  2. Everything after it is the file name.
  3. substr builds "c.txt".

Example 3 — rfind Returns npos

Missing text yields npos.

C++
#include <iostream>
#include <string>

int main() {
    std::string s = "abc";
    if (s.rfind("z") == std::string::npos) {
        std::cout << "no z\\n";
    }
    return 0;
}

How It Works

  1. There is no "z".
  2. rfind returns npos.
  3. The if-check prints the message.

Frequently Asked Questions

It finds the last occurrence of a substring or character.
std::string::npos.
find returns the first match; rfind returns the last.
It limits how far right a match may start.
Yes.

Next: shrink_to_fit()

Keep going through the string functions. Non-binding request to reduce capacity toward size().

shrink_to_fit() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful