To find a single character in a C++ string, pass that character to find(). The function returns the index of the first match, or std::string::npos if the character is not in the string. Searches are case-sensitive, so 'O' and 'o' are different characters.
Find a character with find()
#include <iostream>
#include <string>
int main() {
std::string text = "hello world";
std::string::size_type index = text.find('o');
if (index != std::string::npos) {
std::cout << "First 'o' is at index " << index << '\n';
} else {
std::cout << "No 'o' found\n";
}
return 0;
}
This prints First 'o' is at index 4. Counting from zero, h is at index 0, e is at 1, the first l is at 2, the second l is at 3, and o is at 4.
The single quotes matter. 'o' is a char, which selects the one-character overload. "o" is a string, and it also works, but the char version says what you mean.
Check the result against npos
Always compare the result with std::string::npos before you use it. If the character is missing, find() returns npos. That value is outside the string, and using it as an index is undefined behavior.
Find the next occurrence
To find every occurrence, call find() again with a start position one past the last match:
std::string text = "hello world";
std::string::size_type pos = text.find('o');
while (pos != std::string::npos) {
std::cout << "'o' at index " << pos << '\n';
pos = text.find('o', pos + 1);
}
This prints 'o' at index 4 and then 'o' at index 7.
Find any one of several characters
find_first_of() returns the first position of any character in a set. It is useful when more than one character can separate parts of a line:
std::string line = "name=alice";
std::cout << line.find_first_of("=:") << '\n'; // 4
Use std::find with iterators
If your code already works with iterators, std::find from <algorithm> is another option. It returns an iterator, and subtracting begin() gives the index:
#include <algorithm>
#include <iostream>
#include <string>
int main() {
std::string text = "hello world";
auto it = std::find(text.begin(), text.end(), 'w');
if (it != text.end()) {
std::cout << "'w' at index " << (it - text.begin()) << '\n'; // 6
}
return 0;
}
Search a C-style string with strchr()
A plain char array has no find() member function. Use std::strchr() from <cstring> instead. It returns a pointer to the first match, or nullptr if there is none:
#include <cstring>
#include <iostream>
int main() {
const char* text = "hello";
const char* hit = std::strchr(text, 'l');
if (hit != nullptr) {
std::cout << "'l' at index " << (hit - text) << '\n'; // 2
}
return 0;
}
Common mistakes
- Comparing against -1.
index == -1can appear to work, butstd::string::npossays what you mean. Store the result instd::string::size_typeorauto. - Forgetting that the search is case-sensitive.
text.find('O')does not find a lowercase'o'. - Using the index without checking it. Check for
nposfirst, as shown above.
To check every character one by one, rather than searching for one, see how to iterate through a string in c.