c++ if string in string code example

Example 1: c++ string contains

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

Example 2: check if character in string c++

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

Example 3: c++ contains

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

Example 4: find substring in string c++

std::string parentstring = "Hello Agnosticdev, I love Tutorials";
std::string substring = "Agnosticdev";
auto index = parentstring.find(substring);

Tags:

Cpp Example