Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Strings and I/O

C++ string::find and rfind

Search inside a std::string with find, rfind, find_first_of, find_last_of and friends. Learn npos, search positions, contains and starts_with.

find searches a std::string for a substring or a character and returns the index of the first match, or the constant std::string::npos when nothing is found. rfind does the same from the right and returns the start of the last match. A family of related functions (find_first_of, find_last_of, find_first_not_of, find_last_not_of) look for any character from a set instead of a whole substring.

The most important rule: always compare the result with npos. npos is the largest value of size_t, so using the result as an index without checking is a bug, and storing it in an int turns it into -1 by accident.

C++
std::string s = "one two one";
s.find("one");      // 0
s.rfind("one");     // 8
s.find("six");      // std::string::npos

Try it

C++ find and rfind

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Character-set searches

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
size_type find(const basic_string& str, size_type pos = 0) const noexcept;
size_type find(const char* s, size_type pos, size_type count) const;
size_type find(const char* s, size_type pos = 0) const;
size_type find(char ch, size_type pos = 0) const noexcept;
size_type rfind(/* same overloads */, size_type pos = npos) const;
size_type find_first_of(/* same overloads */, size_type pos = 0) const;
size_type find_last_of(/* same overloads */, size_type pos = npos) const;
size_type find_first_not_of(/* same overloads */, size_type pos = 0) const;
size_type find_last_not_of(/* same overloads */, size_type pos = npos) const;
bool contains(/* string_view-like, const char*, char */) const;      // C++23
Name Description
str, s, ch What to look for: a string, a C string (null-terminated, or count characters of it), or one character. For the _of functions it is a set of characters.
pos Where the search starts. For find and find_first_* it is the lowest allowed index; for rfind and find_last_* it is the highest index a match may start at.
count Length of the C-string needle when given explicitly.

Returns: the index of the match, or std::string::npos.

Errors: none are thrown (the functions are effectively noexcept); a pos past the end simply finds nothing (an empty needle at pos == size() is found there).

Usage notes

  • Check npos, not -1 or < 0. Store the result in std::size_t (or auto).
  • rfind and pos: the match must begin at or before pos, not end there.
  • Empty needle: find("") returns pos (if pos <= size()).
  • Case sensitivity: all searches compare exactly; for case-insensitive search convert both sides first.
  • Complexity: find is O(nยทm) in the worst case; libstdc++ uses fast scanning for the first character, and C++17 std::boyer_moore_searcher is available for very long texts.
  • Yes/no questions: contains (C++23), starts_with and ends_with (C++20) are clearer than find(...) != npos. Each also exists on std::string_view.
  • Not a regular expression. Use <regex> or hand-written parsing for patterns.
  • Searching for a character class (whitespace, digits) is a job for find_first_of/find_first_not_of or std::find_if with a predicate.