The strContains function returns true or false depending on whether the input string contains the search string. If you have ever used the contains function in C# or VB then you may know how useful it can be.
bool strContains(const string inputStr, const string searchStr)
{
size_t contains;
contains = inputStr.find(searchStr);
if(contains != string::npos)
return true;
else
return false;
}
/* Example */
#include <iostream>
#include <string>
using namespace std;
bool strContains(const string inputStr, const string searchStr)
{
size_t contains;
contains = inputStr.find(searchStr);
if(contains != string::npos)
return true;
else
return false;
}
int main()
{
string sentence = "Hello World, This is a test of the contains function";
if(strContains(sentence, "contains function"))
{
cout << "It works!" << endl;
}
else
cout << "Oops, time to rethink" << endl;
return 0;
}
/* End of example */
Comments :
0 comments to “String Contains function”
Post a Comment