BlackIntel Forum

BlackIntel => General discussion => Topic started by: reborn on February 28, 2010, 01:33:38 pm

Title: Strings, because words mean nothing!
Post by: reborn 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?
Title: Re: Strings, because words mean nothing!
Post by: StealthEye 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?
Title: Re: Strings, because words mean nothing!
Post by: reborn 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