The find() member function is the usual way to search a string in C++. It looks for a piece of text inside a std::string and returns the position where the match starts. Positions count from zero. If nothing matches, it returns std::string::npos, a constant that means “no position.” Searches are case-sensitive, so "Cat" does not match "cat".
Syntax and common overloads
std::string::size_type find(const std::string& str, std::string::size_type pos = 0) const;
std::string::size_type find(const char* s, std::string::size_type pos = 0) const;
std::string::size_type find(char ch, std::string::size_type pos = 0) const;
str,s, orchis what you are looking for: a string, a C-style string, or a single character.posis the index where the search starts. It defaults to0, the beginning of the string.
There is also a find(s, pos, count) overload, which searches for only the first count characters of a C-style string. You will not need it often.
A basic search
#include <iostream>
#include <string>
int main() {
std::string text = "The quick brown fox";
std::string::size_type index = text.find("quick");
if (index != std::string::npos) {
std::cout << "Found at index " << index << '\n'; // Found at index 4
} else {
std::cout << "Not found\n";
}
return 0;
}
The q in “quick” is at index 4, because the first character, T, is at index 0.
Always check for npos
Compare the result with std::string::npos before you use it as an index. An index equal to npos is outside the string, and using it with operator[] is undefined behavior. The at() function throws std::out_of_range instead, which makes the mistake easier to spot.
Store the result in std::string::size_type or auto, not int. An int often still works, because -1 converts to npos, but the compiler may warn about comparing signed and unsigned values.
Start the search at a position
The second argument tells find() where to start. This is how you find the next match after one you already have.
std::string text = "banana";
std::string::size_type first = text.find("an"); // 1
std::string::size_type second = text.find("an", 2); // 3
To visit every match, start each search one character after the last match:
std::string text = "banana";
std::string::size_type pos = text.find("an");
while (pos != std::string::npos) {
std::cout << "Match at " << pos << '\n';
pos = text.find("an", pos + 1);
}
This prints Match at 1 and Match at 3. Starting one character after each match also finds overlapping matches, such as the two pairs in "aaa" that start at index 0 and index 1.
Search backward with rfind()
rfind() starts from the end of the string and returns the position of the last match, or npos if there is none.
std::string text = "banana";
std::cout << text.rfind("an") << '\n'; // 3
Its optional second argument is the last index where a match may start.
Find one of several characters
find_first_of() returns the first position of any character in a given set. find_first_not_of() returns the first position of a character that is not in the set.
std::string line = "name=alice";
std::cout << line.find_first_of("=:") << '\n'; // 4
std::string padded = " padded";
std::cout << padded.find_first_not_of(' ') << '\n'; // 3
The second example shows one way to find where the text starts after leading spaces. find_last_of() and find_last_not_of() do the same jobs, but they search from the end of the string.
Search for a single character
A single character works with find() too. Pass a char, such as 'o', and the search looks for that one character. For a step-by-step guide to that case, see how to locate a character in a string.
Searching C-style strings
A plain char array is not a std::string, so it has no find() member function. The <cstring> header provides functions for it instead. strchr() finds a single character, and strstr() finds a substring. Both return a pointer to the match, or nullptr if nothing matches. Subtracting the start pointer gives an index.
#include <cstring>
#include <iostream>
int main() {
const char* text = "hello";
const char* hit = std::strchr(text, 'l');
if (hit != nullptr) {
std::cout << "Index " << (hit - text) << '\n'; // Index 2
}
return 0;
}
For more on strstr(), see what does strstr() do in C.
string_view and contains()
std::string_view also has a find() member function, with the same return values. It is useful when you want to search text without copying it. In C++23 and later, contains() on std::string and std::string_view returns a bool, which is simpler when you only need a yes or no answer.
Common mistakes
- Skipping the npos check. Using the result as an index without comparing it to
nposcan read outside the string. - Expecting a match in a different case.
find("cat")does not match"Cat". - Expecting an iterator.
find()returns an index, not an iterator. To get an iterator, add the index tobegin(), such astext.begin() + index, and check fornposfirst. - Using find() for the last match. It returns the first match. Use
rfind()for the last one.
Split a string at a delimiter
Finding a delimiter, such as a comma, is the first step in splitting a string. Call find() to get each delimiter’s position, then call substr() to take the text between two positions. For the full loop, see how to split a string based on a delimiter in C++.
Key takeaways
find()returns the index of the first match, orstd::string::nposif nothing matches.- Check for
nposbefore you use the index. - Pass a start position to find the next match, and use
rfind()to search from the end. - Use
find_first_of()orfind_first_not_of()when you are looking for a set of characters.