Home C++ Strings Functions rfind() C++ std::string rfind() Function 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.
auto p = s.rfind("na");
auto q = s.rfind('a', 3); Call What 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 size_t rfind(const string& str, size_t pos = npos) const;
size_t rfind(char ch, size_t pos = npos) const; Parameters Parameter Description str / chText or character to find. posOptional upper bound for the start index of a match.
Return Value Return Value Description Index Start position of the last match. nposWhen not found.
C++ Examples 1. Last match 2. Last char 3. Not found Example 1 — Basic rfind() Find the last "na" in banana.
#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 "banana" contains "na" at 2 and 4.rfind returns the last start index, 4. find would have returned 2. Example 2 — rfind a Character Find the last slash in a path.
#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 The last '/' is at index 5. Everything after it is the file name. substr builds "c.txt". Example 3 — rfind Returns npos Missing text yields npos.
#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 There is no "z". rfind returns npos. The if-check prints the message. Frequently Asked Questions What does rfind do? It finds the last occurrence of a substring or character.
What does it return if missing? std::string::npos.
How is rfind different from find? find returns the first match; rfind returns the last.
What does the pos argument mean? It limits how far right a match may start.
Is rfind case-sensitive? 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.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion