C++ std::string find_last_not_of() Function

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

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.

C++
auto p = s.find_last_not_of(" \\t");
CallWhat 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
C++
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

ParameterDescription
setCharacters to ignore.
posOptional upper index bound.

Return Value

Return ValueDescription
Index or nposLast character not in the set.

C++ Examples

Example 1 — Basic find_last_not_of

Trim trailing spaces.

C++
#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

  1. Last non-space is 'i'.
  2. erase from the next index removes trailing spaces.
  3. Brackets show the trimmed result.

Example 2 — Last Non-Zero

Find last non-zero digit char.

C++
#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

  1. Trailing zeros are ignored.
  2. Last non-zero is '2' at index 2.
  3. Useful for simple numeric text cleanup.

Example 3 — All In Set

Only spaces → npos.

C++
#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

  1. Every character is a space.
  2. No qualifying character exists.
  3. Comparison with npos is true.

Frequently Asked Questions

Finds the last character that is not in a set.
Yes—find last non-space then erase the tail.
Returns npos.
rfind looks for a specific substring/char; find_last_not_of looks for anything outside a set.
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.

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.

14 people found this page helpful