Not logged in

Please login with username, password and session length or register.


Advanced search  

Author Topic: Strings, because words mean nothing!  (Read 17517 times)

reborn

  • Engineer
  • **
  • Offline
  • Posts: 51
Strings, because words mean nothing!
« on: February 28, 2010, 01:33:38 pm »

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:

Code: [Select]
if(strd[0] == '&'){

So I thought I'd try this:

Code: [Select]
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:

[quote title=Quote:]
#lobby
#
#
[/quote]

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?
Logged

StealthEye

  • Founder
  • Staff
  • Medium Tank driver
  • ******
  • Offline
  • Posts: 939
Re: Strings, because words mean nothing!
« Reply #1 on: February 28, 2010, 05:47:15 pm »

What you posted should be correct:
Code: [Select]
if (strd[0] == '&')If it is not working, there must be some other cause... Maybe copy and paste a bigger chunk of code?
Logged

reborn

  • Engineer
  • **
  • Offline
  • Posts: 51
Re: Strings, because words mean nothing!
« Reply #2 on: March 01, 2010, 12:13:23 am »

DanPaul suggested this "if((strd.c_str())[0] == '&'){". It worked. :-)

Still not sure why if(strd[0] == '&'){ always returned true. :S
Logged
 

April 26, 2024, 09:10:08 pm