Home C++ Strings Functions find_first_not_of() C++ std::string find_first_not_of() Function 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.
auto p = s.find_first_not_of(' ');
auto q = s.find_first_not_of("0"); Call What 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 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 Parameter Description set Characters that are allowed / skipped. posStart index.
Return Value Return Value Description Index or npos First disallowed character position.
C++ Examples 1. Skip spaces 2. Skip zeros 3. All matched Example 1 — Basic find_first_not_of Skip leading spaces.
#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 Leading spaces are in the skip set. First non-space is 'h'. substr from there yields "hi". Example 2 — Skip Leading Zeros Find first non-zero digit character.
#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 Zeros are skipped. Index of '4' is returned. substr prints 42. Example 3 — All In Set → npos String of only spaces.
#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 Every character is a space. No “not of” character exists. npos triggers the blank message. Frequently Asked Questions What does find_first_not_of do? Finds the first character that is not in the provided set.
Good for trimming? Yes—for leading characters you want to skip.
What if the string is only set chars? Returns npos.
Difference from find_first_of? find_first_of wants a character in the set; find_first_not_of wants one outside the set.
Related? 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.
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.
12 people found this page helpful
Helpful Share Copy link Suggestion