
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ String::find_first_not_of() function
The C++ std::string::find_first_not_of() function is used to locate the first character in the string that does not match any character from a specified set. It takes a string or a character set as its parameter and returns the index of the first character in the original string that is not found in the given set.
If all the characters in the string are in the set, then it returns std::string::npos.
Syntax
Following is the syntax for std::string::find_first_not_of() function.
size_t find_first_not_of (const string& str, size_t pos = 0) const; or size_t find_first_not_of (const char* s, size_t pos = 0) const; or size_t find_first_not_of (const char* s, size_t pos, size_t n) const; or size_t find_first_not_of (char c, size_t pos = 0) const;
Parameters
- str − It indicates the string object.
- s − It indicates the pointer to an array of characters.
- pos − It indicates the position of the first character in the string.
- n − It indicates the number of characters values to search for.
- c − It indicates the individual character to be searched for.
Return value
This function returns the position of the first character that does not match.
Example 1
In the following example, we are going to consider the basic usage of the find_first_not_of() function.
#include <iostream> #include <string> using namespace std; int main() { string str(" It looks for non-alphabetic characters"); size_t found = str.find_first_not_of("abcdefghijklmnopqrstuvwxyz "); if (found != std::string::npos) { cout << " The first non-alphabetic character is " << str[found]; cout << " at index " << found << '\n'; } return 0; }
Output
Let us compile and run the above program, this will produce the following result −
The first non-alphabetic character is I at index 1
Example 2
Consider the following example, where we are going to find the first non-digit in a string.
#include <iostream> #include <string> using namespace std; int main() { string str = " 12345abc6789"; size_t pos = str.find_first_not_of(" 0123456789"); if (pos != string::npos) { cout << " First non-digit is at index " << pos << endl; } return 0; }
Output
If we run the above code it will generate the following output.
First non-digit is at index 6
Example 3
In the following example, we are going to skip the whitespace at the beginning of the string.
#include <iostream> #include <string> using namespace std; int main() { string str = " Hello World! "; size_t pos = str.find_first_not_of(" \t\n "); if (pos != string::npos) { cout << " First non-whitespace character is at index " << pos << endl; } return 0; }
Output
Following is the output of the above code.
First non-whitespace character is at index 4
Example 4
Let's look at the following example, where we going to initialize two strings to find the first character not in another string.
#include <iostream> #include <string> using namespace std; int main() { string str1 = " abcdefg"; string str2 = " abc"; size_t pos = str1.find_first_not_of(str2); if (pos != string::npos) { cout << "First character in '" << str1 << "' not in '" << str2 << "' is '" << str1[pos] << "' at index " << pos << endl; } else { cout << "All characters in '" << str1 << "' are found in '" << str2 << "'" << endl; } return 0; }
Output
Following is the output of the above code.
First character in ' abcdefg' not in ' abc' is 'd' at index 4