C++ Check If String Contains Substring

5 min read Jul 01, 2024
C++ Check If String Contains Substring

C++: Checking if a String Contains a Substring

This article will guide you through different methods in C++ to check if a given string contains a specific substring.

1. Using the find() method:

The find() method from the <string> library is a straightforward and commonly used approach. It searches for the first occurrence of a substring within a string and returns its starting position.

#include 
#include 

int main() {
  std::string str = "This is a test string.";
  std::string substring = "test";

  // Using find()
  size_t found = str.find(substring);

  if (found != std::string::npos) {
    std::cout << "Substring '" << substring << "' found at position: " << found << std::endl;
  } else {
    std::cout << "Substring '" << substring << "' not found." << std::endl;
  }

  return 0;
}

Explanation:

  • str.find(substring) searches for the substring substring within the string str.
  • If the substring is found, find() returns its starting position as a size_t value.
  • If not found, it returns std::string::npos, which is a special value indicating the substring was not present.

2. Using the strstr() function:

The strstr() function from the <cstring> library is a C-style function that searches for a substring within a null-terminated character array.

#include 
#include 

int main() {
  char str[] = "This is a test string.";
  char substring[] = "test";

  // Using strstr()
  char* result = strstr(str, substring);

  if (result != nullptr) {
    std::cout << "Substring '" << substring << "' found." << std::endl;
  } else {
    std::cout << "Substring '" << substring << "' not found." << std::endl;
  }

  return 0;
}

Explanation:

  • strstr(str, substring) searches for the substring substring within the character array str.
  • If found, it returns a pointer to the first occurrence of substring within str.
  • If not found, it returns nullptr.

3. Using the search() method (Regular Expressions):

The search() method from the <regex> library allows you to use regular expressions to find patterns within a string. This provides more flexibility than the previous methods.

#include 
#include 

int main() {
  std::string str = "This is a test string.";
  std::regex regex("test");

  // Using regex_search()
  std::smatch match;
  if (std::regex_search(str, match, regex)) {
    std::cout << "Substring '" << match[0] << "' found." << std::endl;
  } else {
    std::cout << "Substring 'test' not found." << std::endl;
  }

  return 0;
}

Explanation:

  • std::regex regex("test") creates a regular expression object that matches the literal substring "test".
  • std::regex_search(str, match, regex) searches for a match of the regex pattern within the string str and stores the match in the match object.
  • If a match is found, match[0] will contain the matched substring.

Choosing the right method:

  • If you need simple substring checks, the find() method is generally the best choice due to its ease of use and efficiency.
  • If you are working with C-style character arrays, strstr() might be more suitable.
  • For more complex pattern matching, regular expressions using regex_search() offer greater flexibility.

Remember to choose the method that best suits your specific needs and coding style.

Latest Posts