Home C++ Strings Functions find_last_not_of() C++ std::string find_last_not_of() Function Definition In C++, std::string::find_last_not_of() finds the last character that is not in a 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 characters to ignore from the end.
auto p = s.find_last_not_of(" \\t"); Call What it does s.find_last_not_of(set)Last index outside set. s.find_last_not_of(set, pos)Only consider indices ≤ pos.
Classic trailing trim: erase from p+1 to end after finding last non-space.
Library declarations size_t find_last_not_of(const string& str, size_t pos = npos) const;
size_t find_last_not_of(char ch, size_t pos = npos) const; Parameters Parameter Description set Characters to ignore. posOptional upper index bound.
Return Value Return Value Description Index or npos Last character not in the set.
C++ Examples 1. Trim trailing 2. Trailing zeros 3. All skipped Example 1 — Basic find_last_not_of Trim trailing spaces.
#include <iostream>
#include <string>
int main() {
std::string s = "hi ";
std::size_t p = s.find_last_not_of(' ');
s.erase(p + 1);
std::cout << '[' << s << "]\\n";
return 0;
} How It Works Last non-space is 'i'. erase from the next index removes trailing spaces. Brackets show the trimmed result. Example 2 — Last Non-Zero Find last non-zero digit char.
#include <iostream>
#include <string>
int main() {
std::string s = "1200";
std::size_t p = s.find_last_not_of('0');
std::cout << p << ' ' << s[p] << '\\n';
return 0;
} How It Works Trailing zeros are ignored. Last non-zero is '2' at index 2. Useful for simple numeric text cleanup. Example 3 — All In Set Only spaces → npos.
#include <iostream>
#include <string>
int main() {
std::string s = " ";
std::cout << std::boolalpha
<< (s.find_last_not_of(' ') == std::string::npos) << '\\n';
return 0;
} How It Works Every character is a space. No qualifying character exists. Comparison with npos is true. Frequently Asked Questions What does find_last_not_of do? Finds the last character that is not in a set.
Trim use case? Yes—find last non-space then erase the tail.
What if all characters are in the set? Returns npos.
Difference from rfind? rfind looks for a specific substring/char; find_last_not_of looks for anything outside a set.
Related? find_first_not_of for the leading side.
Next: find_last_of() Keep going through the string functions. Find the last character that matches any in a set.
find_last_of() 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.
14 people found this page helpful
Helpful Share Copy link Suggestion