tags:

views:

348

answers:

2

I have a variable of type string. I want to check if it contains a certain string. How would I do that?

Is there a function that returns true if the string is found, and false if it isn't?

+9  A: 

Perhaps the following:

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

Note: "found!" will be printed if s2 is a substring of s1, both s1 and s2 are of type std::string.

Beh Tou Cheh
A: 

You can try using the find function:

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

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