check the existence of string

G

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
 
V

Victor Bazarov

Gary said:
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
 
G

Gary Wessle

Victor Bazarov said:
Gary said:
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

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
 
V

Victor Bazarov

Gary said:
Victor Bazarov said:
Gary said:
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

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
 
G

Gary Wessle

Victor Bazarov said:
Gary said:
Victor Bazarov said:
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

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
 
V

Victor Bazarov

Gary said:
Victor Bazarov said:
Gary said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top