364,036 Members | 5027 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

check the existence of string

Gary Wessle
P: n/a
Gary Wessle

hi

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;

thanks
Aug 8 '06 #1
Share this Question
Share on Google+
5 Replies


Victor Bazarov
P: n/a
Victor Bazarov
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


Aug 8 '06 #2

Gary Wessle
P: n/a
Gary Wessle
"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
Aug 8 '06 #3

Victor Bazarov
P: n/a
Victor Bazarov
Gary Wessle wrote:
"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
Well, you're trying to have a statement inside an expression.
You simply cannot do that.. What you probably want is

return s.find(t) != string::npos ? 2.0 : 4.0 ;

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


Aug 8 '06 #4

Gary Wessle
P: n/a
Gary Wessle
"Victor Bazarov" <v.Abazarov@comAcast.netwrites:
Gary Wessle wrote:
>"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
>
Well, you're trying to have a statement inside an expression.
You simply cannot do that.. What you probably want is
>
return s.find(t) != string::npos ? 2.0 : 4.0 ;
>
that returns -5.20371e-39

it is only when I do

double y;
(s.find(t,0))!= string::npos ? y=2.0 : y=4.0;
return y;
that it works
Aug 9 '06 #5

Victor Bazarov
P: n/a
Victor Bazarov
Gary Wessle wrote:
"Victor Bazarov" <v.Abazarov@comAcast.netwrites:
>
>Gary Wessle wrote:
>>"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
>>
>Well, you're trying to have a statement inside an expression.
>You simply cannot do that.. What you probably want is
>>
> return s.find(t) != string::npos ? 2.0 : 4.0 ;
>>
that returns -5.20371e-39
Really?!
it is only when I do
>
double y;
(s.find(t,0))!= string::npos ? y=2.0 : y=4.0;
return y;
that it works
I think that's covered in the FAQ 5.8.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


Aug 9 '06 #6

Post your reply

Help answer this question



Didn't find the answer to your C / C++ question?

You can also browse similar questions: C / C++