"Victor Bazarov" <v.Abazarov@comAcast.netwrites:
Gary Wessle wrote:
>or is there a better way to check the existence of "py" in a string
>"s"?
>>
> string s = "djrfpyfd";
> string t = "py";
> string r = s.substr(s.find("py"),2);
> cout << (t==r) << endl;
>
'find' returns 'std::string::npos' if the [sub]string is NOT found. So,
if you just wanted to check the existence, you just need to check the
return value of 'find'. There is no need to extract and compare. RTFM
>
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
the fix:
string s = "djrfpyfd";
string t = "pyd";
if ( (s.find(t,0))!= string::npos )
cout << "found" << endl;
however the next does not work
#include <string>
using std::string;
( (s.find(t,0))!= string::npos ) ? return 2.0 : return 4.0;
I get
: error: expected primary-expression before 'return'
: error: expected `:' before 'return'
: error: expected primary-expression before 'return'
: error: expected `;' before 'return'
: error: expected primary-expression before ':' token
: error: expected `;' before ':' token