string contains other string c++ 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: c++ contains

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

Example 4: find in string c++

size_t find (const string& str, size_t pos = 0) const;

Tags:

Java Example