Check if a string contains another string in C++
This post will discuss how to check if a string contains another string in C++.
1. Using string::find
A simple solution is to use the string::find algorithm to search the specified substring in the string. It returns the index of the first instance of the specified substring or string::npos if the substring is not present.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <string> int main() { std::string haystack = "It is like looking for a needle in a haystack"; std::string needle = "needle"; bool found = haystack.find(needle) != std::string::npos; if (found) { std::cout << "String found" << std::endl; } else { std::cout << "String not found" << std::endl; } return 0; } |
Output:
String found
2. Using Boost
If the boost library is available, try using the boost::algorithm::contains algorithm in the header <boost/algorithm/string.hpp>. This function can be invoked as:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> #include <string> #include <boost/algorithm/string.hpp> int main() { std::string haystack = "It is like looking for a needle in a haystack"; std::string needle = "needle"; bool found = boost::algorithm::contains(haystack, needle); if (found) { std::cout << "String found" << std::endl; } else { std::cout << "String not found" << std::endl; } return 0; } |
Output:
String found
3. Using strstr() function
Finally, you can convert both strings to C-strings using the c_str() function and compare them using the strstr() function. It returns a pointer to the first occurrence of the substring in the given string, or a null pointer if the substring is not found.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> #include <string> #include <cstring> int main() { std::string haystack = "It is like looking for a needle in a haystack"; std::string needle = "needle"; const char* found = strstr(haystack.c_str(), needle.c_str()); if (found) { std::cout << "String found" << std::endl; } else { std::cout << "String not found" << std::endl; } return 0; } |
Output:
String found
4. Using basic_string::contains
With C++23, you can use the much-awaited basic_string::contains function to check whether a string contains the given substring. It returns true if the string contains the specified substring, and false otherwise.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <string> int main() { std::string haystack = "It is like looking for a needle in a haystack"; std::string needle = "needle"; if (haystack.contains(needle)) { std::cout << "String found" << std::endl; } else { std::cout << "String not found" << std::endl; } return 0; } |
Output:
String found
That’s all about checking if a string contains another string in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)