Home C++ Strings Functions find_first_of() C++ std::string find_first_of() Function Definition In C++, std::string::find_first_of() scans left to right for the first character that appears in a set of characters .
If none match, the result is npos. 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 set of characters to look for.
auto p = s.find_first_of("aeiou");
auto q = s.find_first_of(" ,.", 3); Call What it does s.find_first_of(set)First index whose character is in set. s.find_first_of(set, pos)Same, starting the search at pos.
The argument is a set of individual characters, not a substring to match as a whole. For substrings use find.
Library declarations size_t find_first_of(const string& str, size_t pos = 0) const;
size_t find_first_of(char ch, size_t pos = 0) const; Parameters Parameter Description str / setCharacters that count as a match. posWhere to start scanning.
Return Value Return Value Description Index or npos Position of the first matching character.
C++ Examples 1. First vowel 2. First punct 3. Start later Example 1 — Basic find_first_of Find the first vowel.
#include <iostream>
#include <string>
int main() {
std::string s = "sky";
std::size_t p = s.find_first_of("aeiou");
if (p == std::string::npos) {
std::cout << "no vowel\\n";
} else {
std::cout << "at " << p << '\\n';
}
return 0;
} How It Works Characters checked against the vowel set. None of s,k,y are vowels. Result is npos. Example 2 — Find Separator Find the first space or comma.
#include <iostream>
#include <string>
int main() {
std::string s = "one,two";
std::size_t p = s.find_first_of(" ,");
std::cout << p << ' ' << s[p] << '\\n';
return 0;
} How It Works Scan finds ',' at index 3. That character is in the set. Useful for simple tokenization starts. Example 3 — Start at pos Skip the first match by starting later.
#include <iostream>
#include <string>
int main() {
std::string s = "a-b-c";
std::size_t p1 = s.find_first_of('-');
std::size_t p2 = s.find_first_of('-', p1 + 1);
std::cout << p1 << ' ' << p2 << '\\n';
return 0;
} How It Works First dash is at 1. Second search starts after it. Next dash is at 3. Frequently Asked Questions What does find_first_of do? It finds the first character that matches any character in a given set.
Is the argument a substring? No. Each character in the argument is a candidate match.
What if nothing matches? Returns npos.
How do I find a whole word? Use find(), not find_first_of().
Related opposite? find_first_not_of finds the first character not in the set.
Next: find_last_not_of() Keep going through the string functions. Find the last character that is not in a given set.
find_last_not_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.
13 people found this page helpful
Helpful Share Copy link Suggestion