C++ string find(): Find a Substring or Character in a String
Short answer: text.find("cat") returns the position of the first match, counting from 0, or std::string::npos if there’s no match. Always compare the result with npos before you use it:
if (text.find("cat") != std::string::npos) {
// found
}
The rest of this guide covers searching for a character, starting from a position, finding every match, searching backwards with rfind, and the if (text.find(...)) mistake that catches almost everyone.
Finding a Substring
#include <iostream>
#include <string>
int main() {
std::string text = "the cat sat on the mat";
std::size_t pos = text.find("cat");
std::cout << pos << '\n';
}
4
Positions start at 0, so "cat" begins at index 4: t=0, h=1, e=2, space=3, c=4. Store the result in a std::size_t, the type find returns.
When There’s No Match: npos
#include <iostream>
#include <string>
int main() {
std::string text = "the cat sat on the mat";
std::size_t pos = text.find("dog");
if (pos == std::string::npos) {
std::cout << "Not found\n";
} else {
std::cout << "Found at " << pos << '\n';
}
}
Not found
npos is a special value meaning “no position”. If you print it, you see the largest number a std::size_t can hold:
#include <iostream>
#include <string>
int main() {
std::string text = "the cat sat on the mat";
std::cout << text.find("dog") << '\n';
}
18446744073709551615
That’s why using the result without checking is dangerous: passing it to substr or using it as an index goes wrong.
The Mistake: if (text.find(…))
This looks reasonable, but it’s wrong:
#include <iostream>
#include <string>
int main() {
std::string text = "the cat sat on the mat";
if (text.find("the")) {
std::cout << "Found\n";
} else {
std::cout << "Not found?\n";
}
}
Not found?
"the" is at position 0, and if (0) is false. Worse, a missing word returns npos, which counts as true. find returns a position, not a yes or no, so compare it with npos:
#include <iostream>
#include <string>
int main() {
std::string text = "the cat sat on the mat";
if (text.find("the") != std::string::npos) {
std::cout << "Found\n";
}
}
Found
If you only need a yes or no, see check if a string contains a substring, which also covers C++23’s contains().
Curious what's happening underneath? Arrays, strings and vectors make much more sense once you understand pointers and memory. C++ Better Explained explains both with diagrams and plain-English analogies.
Get the book — $27Finding a Character, or Searching From a Position
find also takes a single character, and an optional second argument says where to start looking:
#include <iostream>
#include <string>
int main() {
std::string text = "the cat sat on the mat";
std::cout << text.find('s') << '\n';
std::cout << text.find("at", 6) << '\n';
}
8
9
The first line finds the s in “sat”. The second skips the “at” inside “cat” (position 5) because the search starts at position 6.
Finding Every Match
Use that start position in a loop, beginning one past the previous match each time:
#include <iostream>
#include <string>
int main() {
std::string text = "the cat sat on the mat";
std::size_t pos = text.find("at");
while (pos != std::string::npos) {
std::cout << "\"at\" at " << pos << '\n';
pos = text.find("at", pos + 1);
}
}
"at" at 5
"at" at 9
"at" at 20
The same loop is the first step of replacing every occurrence and splitting a string.
Searching From the End: rfind
rfind returns the last match instead of the first:
#include <iostream>
#include <string>
int main() {
std::string text = "the cat sat on the mat";
std::cout << text.rfind("the") << '\n';
std::cout << text.find("the") << '\n';
}
15
0
It’s handy for file names and paths, where the part you want comes after the last separator:
#include <iostream>
#include <string>
int main() {
std::string path = "C:/games/save.txt";
std::size_t slash = path.rfind('/');
std::cout << path.substr(slash + 1) << '\n';
}
save.txt
In real code, check slash != std::string::npos first, in case there’s no / at all. More on cutting strings in how to get a substring.
find_first_of and find_first_not_of
These search for any one of several characters:
#include <iostream>
#include <string>
int main() {
std::string word = "rhythm and blues";
std::string padded = " hello";
std::cout << word.find_first_of("aeiou") << '\n';
std::cout << padded.find_first_not_of(' ') << '\n';
}
7
3
find_first_of("aeiou") finds the first vowel, the a in “and”. find_first_not_of(' ') skips the leading spaces, which is how trimming a string works. There are matching find_last_of and find_last_not_of that search from the end.
Quick Reference
| You want | Use | Returns when nothing’s found |
|---|---|---|
| First match of a substring | text.find("cat") | std::string::npos |
| First match of a character | text.find('s') | std::string::npos |
First match at or after position p | text.find("at", p) | std::string::npos |
| Last match | text.rfind("the") | std::string::npos |
| First of any of these characters | text.find_first_of("aeiou") | std::string::npos |
| First character not in a set | text.find_first_not_of(' ') | std::string::npos |
| Just yes or no | text.find("x") != std::string::npos |
find is case-sensitive: "Cat" won’t match "cat". For a case-insensitive search, see the last section of string contains substring.
Take Your C++ Further

Arrays, strings and vectors are easier to use well once you know what's underneath them: pointers and memory. C++ Better Explained explains both, and OOP, in 87 plain-English pages with diagrams and analogies.
Get the book — $2787 pages · plain English · 30-day money-back guarantee
Related Articles
- C++ Check if String Contains Substring — the yes-or-no version, and C++23’s contains().
- How to Get a Substring in C++ — cutting out the part you found.
- How to Replace a Substring in C++ — find, then replace.
- How to Split a String in C++ — breaking text up at a delimiter.
- C++ String Handling — the std::string basics.