c++ check if string is in string code example

Example 1: c++ check if string contains substring

string str ("There are two needles in this haystack.");
string str2 ("needle");

if (str.find(str2) != string::npos) {
//.. found.
}

Example 2: c++ string contains

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

Example 3: check if character in string c++

std::string s = "hell[o";
if (s.find('[') != std::string::npos)
    ; // found
else
    ; //

Tags:

Cpp Example