Check if a string contains a string in C++

Use std::string::find as follows:

if (s1.find(s2) != std::string::npos) {
    std::cout << "found!" << '\n';
}

Note: “found!” will be printed if s2 is a substring of s1, both s1 and s2 are of type std::string.

Leave a Comment