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 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
"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
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
"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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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").
...
|
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.
...
|
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
|
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...
|
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...
|
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...
|
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
...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
| |