Jun 13, 2012 at 8:48pm UTC
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.
Jun 13, 2012 at 8:54pm UTC
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)
Jun 13, 2012 at 8:54pm UTC
Yes, you can use member function find:
if ( mystr.find( "good" ) != std::string::npos )
Jun 13, 2012 at 9:00pm UTC
1 2 3 4
if (mystr.substr("good" ) != std::string::npos)
{
// mystr contains 'good'
}
Last edited on Jun 13, 2012 at 9:00pm UTC
Jul 4, 2012 at 6:32pm UTC
How would I do multiple things inside the line? Like:
if (mystr.find("good" || "well") != std::string::npos
Because that doesn't work.