C++ std::string find_first_not_of() Function

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

Definition

In C++, std::string::find_first_not_of() finds the first character that is not in a given set.

Great for skipping leading spaces or zeros. 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 skip.

C++
auto p = s.find_first_not_of(' ');
auto q = s.find_first_not_of("0");
CallWhat it does
s.find_first_not_of(set)First index whose char is outside set.
s.find_first_not_of(set, pos)Start scanning at pos.

If the whole string is only set characters, you get npos.

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

Parameters

ParameterDescription
setCharacters that are allowed / skipped.
posStart index.

Return Value

Return ValueDescription
Index or nposFirst disallowed character position.

C++ Examples

Example 1 — Basic find_first_not_of

Skip leading spaces.

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

int main() {
    std::string s = "  hi";
    std::size_t p = s.find_first_not_of(' ');
    std::cout << s.substr(p) << '\\n';
    return 0;
}

How It Works

  1. Leading spaces are in the skip set.
  2. First non-space is 'h'.
  3. substr from there yields "hi".

Example 2 — Skip Leading Zeros

Find first non-zero digit character.

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

int main() {
    std::string s = "00042";
    std::size_t p = s.find_first_not_of('0');
    std::cout << s.substr(p) << '\\n';
    return 0;
}

How It Works

  1. Zeros are skipped.
  2. Index of '4' is returned.
  3. substr prints 42.

Example 3 — All In Set → npos

String of only spaces.

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

int main() {
    std::string s = "   ";
    if (s.find_first_not_of(' ') == std::string::npos) {
        std::cout << "blank\\n";
    }
    return 0;
}

How It Works

  1. Every character is a space.
  2. No “not of” character exists.
  3. npos triggers the blank message.

Frequently Asked Questions

Finds the first character that is not in the provided set.
Yes—for leading characters you want to skip.
Returns npos.
find_first_of wants a character in the set; find_first_not_of wants one outside the set.
find_last_not_of for trailing skips.

Next: find_first_of()

Keep going through the string functions. Find the first character that matches any in a set.

find_first_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.

12 people found this page helpful