It don't mean a thing, if you aint got that string! [message #421101] |
Sun, 28 February 2010 06:23 |
|
reborn
Messages: 3231 Registered: September 2004 Location: uk - london
Karma: 0
|
General (3 Stars) |
|
|
I have a std::string (oh noes, not std!). And I want to tell if the first character in that string is an ampersand (&).
The std::String variable is strd.
This conditional returns true always, even if the first character is not an ampersand:
So I thought I'd try this:
const char *character = (const char *)strd.at(0);
printf("%s\n",strd.c_str());
printf("%c\n",strd[0]);
printf("%c\n",character);
if(strstr(character, "~")){
The printf's print the following:
Then at the strstr it crashes (I guess because it's supposed to be comparing strings, not characters).
Can someone suggest an appropriate way to check if the first character of a std::string equal &, please?
|
|
|
|
|
|
Re: It don't mean a thing, if you aint got that string! [message #421113 is a reply to message #421105] |
Sun, 28 February 2010 10:41 |
|
reborn
Messages: 3231 Registered: September 2004 Location: uk - london
Karma: 0
|
General (3 Stars) |
|
|
danpaul88 wrote on Sun, 28 February 2010 10:23 | if((strd.c_str())[0] == '&'){
Perhaps? (NB: I have not tried to compile that, but it *should* work in theory... the brackets around the strd.c_str() may or may not be required depending on the other of precedence.)
Basically you should get the underlying c_str and then get the first index of THAT array instead.
|
Cheers Dan, I did try: strd.at(0).c_str() which was my attempt at trying what you just posted, but it's obviously invalid. I'm at work so cannot try what you just wrote, but will give it a go when I get in.
|
|
|
|
|