Question about "include"

if I program this:

if (mystr == "good")
{
cout <<" response 1";
}
else if (mystr == "bad")
{
cout <<" response 2";
}

is there a way that I can have something like if (mystr includes == "good"). Is that possible? Something like that? Thanks.
You can use the find member function to search a string. If the string is not found it will return std::string::npos so something like this should work:
if (mystr.find("good") != std::string::npos)
Yes, you can use member function find:

if ( mystr.find( "good" ) != std::string::npos )
1
2
3
4
if (mystr.substr("good") != std::string::npos)
{
    // mystr contains 'good'
}
Last edited on

@kbw
1
2
3
4
if (mystr.substr("good") != std::string::npos)
{
    // mystr contains 'good'
} 



substr does not accept an argument of type const char[]
My mistake.
How would I do multiple things inside the line? Like:
if (mystr.find("good" || "well") != std::string::npos
Because that doesn't work.
1
2
3
4
if ( (mystr.find("good")  != std::string::npos) ||
     (mystr.find("well")  != std::string::npos) )
{
}
Topic archived. No new replies allowed.