C++ std::string find_last_of() Function

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

Definition

In C++, std::string::find_last_of() finds the rightmost character that belongs to a given set.

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

Pass the character set.

C++
auto p = path.find_last_of("/\\\\");
CallWhat it does
s.find_last_of(set)Last index with a character in set.
s.find_last_of(set, pos)Only consider positions at most pos.

Often used to find the last path separator.

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

Parameters

ParameterDescription
setCharacters that count as a match.
posOptional maximum index to consider.

Return Value

Return ValueDescription
Index or nposPosition of the last matching character.

C++ Examples

Example 1 — Basic find_last_of

Find the last slash.

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

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

How It Works

  1. Last '/' is found.
  2. substr after it yields the file name.
  3. Common path-parsing pattern.

Example 2 — Last From a Set

Find the last vowel in a word.

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

int main() {
    std::string s = "strength";
    std::size_t p = s.find_last_of("aeiou");
    std::cout << p << ' ' << s[p] << '\\n';
    return 0;
}

How It Works

  1. Vowels in the set are scanned from the right.
  2. The last vowel is 'e' at index 2.
  3. Index and character are printed.

Example 3 — find_last_of npos

No character from the set.

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

int main() {
    std::string s = "xyz";
    if (s.find_last_of("aeiou") == std::string::npos) {
        std::cout << "none\\n";
    }
    return 0;
}

How It Works

  1. No vowels appear.
  2. Function returns npos.
  3. Branch prints none.

Frequently Asked Questions

Finds the last character that matches any character in a set.
Finding the last path separator in a file path.
rfind can search for a multi-character substring; find_last_of matches any single character from a set.
npos.
find_last_not_of.

Next: front()

Keep going through the string functions. Reference to the first character. Undefined on an empty string.

front() 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.

15 people found this page helpful