C++ .find()

Christine_Yang's avatar
Published Apr 4, 2022Updated Dec 21, 2022

The .find() method returns the index of the first occurrence of the specified string or character. If no result is found, string::npos is returned instead.

Note: string::npos will often be shown as the highest possible value of type size_t.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.
    • Beginner Friendly.
      11 hours

Syntax

stringName.find(sequence, position, count)

The following arguments are applied to the .find() method:

  • sequence: the char or string to be searched for.
  • position (optional): the index to start the search at (defaults to 0).
  • count (optional): the number of characters that must match (defaults to the length of sequence).

Examples

The following example searches for izz in the food string:

#include <iostream>
#include <string>
int main() {
std::string food = "pizzabagel";
std::cout << food.find("izz");
// Output: 1
}

A starting index can also be specified:

#include <iostream>
#include <string>
int main() {
std::string food = "pizzabagel";
std::cout << food.find("izz", 2);
}

The output for the snippet above would look like this:

18446744073709551615

The output 18446744073709551615 represents the string::npos constant that indicates a match was not found with the given parameters. The sequence, izz, begins at index 1 while the call to the .find() method begins at index 2. Therefore, it is outside of the search range and a matching index was not found.

Searching for a sequence not in the string also returns string::npos. The following example searches for bad which is not in string food:

#include <iostream>
#include <string>
int main() {
std::string food = "pizzabagel";
std::cout << food.find("bad");
}

The output for the snippet above would look like this:

18446744073709551615

Codebyte Example

However, if the previous example is changed to search for the first two characters of bad instead, then a result is found:

Code
Output

All contributors

Learn C++ on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.
    • Beginner Friendly.
      11 hours