473,395 Members | 1,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

check the existence of string


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
5 3261
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
"Victor Bazarov" <v.********@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
Gary Wessle wrote:
"Victor Bazarov" <v.********@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
"Victor Bazarov" <v.********@comAcast.netwrites:
Gary Wessle wrote:
>"Victor Bazarov" <v.********@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
Gary Wessle wrote:
"Victor Bazarov" <v.********@comAcast.netwrites:
>Gary Wessle wrote:
>>"Victor Bazarov" <v.********@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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: someone | last post by:
Hi All, What is the fastest way to check the existence of a string in a set of strings? I have thought of putting the set of strings in a HashMap and then us containsKey() to do the check but...
2
by: David | last post by:
I'm using following code for checking a file existence. I's working fine for given folder. Is there a way to check a file exitance in subfolders? Thanks in advance, David Option Compare...
1
by: John | last post by:
Hi all, How do I programmatically check for the existence of a DLL in the BIN folder prior to using it? I don't want to hard-code the path (i.e. "C:\InetPub\wwwroot\mysite\bin\MyDLL.dll"). ...
2
by: www.MessageMazes.com | last post by:
Greetings, I'm experimenting with an ASP page that reads data from a file name that is passed to it as a parameter, as in this page, which works, because the "good" file exists. ...
10
by: Dieter Pelz | last post by:
Hallo, what is the best way to check the installation of mfc80 and vcrt sidebyside assemblies? Best Regards, Dieter Pelz
3
by: trint | last post by:
How can I do this with my c# code with my website(because the file is there, but the code doesn't return it)?: if(File.Exists(String.Format("~/images/categories/{0}", sFileName)) return...
2
by: DesCF | last post by:
I have a textbox and a combobox on a toolstrip. The user enters either an ID in the textbox or selects a name from the combobox. When the user selects a name from the combobox the textbox is...
10
by: bluray | last post by:
Hello guys, thanks again for taking the time to help me out with my problems! This problem seems super simple in my head, however getting the coding to make it work is turing out to be not so...
0
by: MIRRA | last post by:
Hi I have a UNIX script which creates a report. In that script I have to define the java classpath. Also I need to check if the java classpath specified exists or not. Below is the command i use ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.