Question about string comparison

Discussion of chess software programming and technical issues.

Moderator: Ras

P. Villanueva
Posts: 85
Joined: Sat May 17, 2008 10:57 pm
Location: Bilbao, Spain

Question about string comparison

Post by P. Villanueva »

Please could anyone explain me the difference between these two codes?

Note: comando is a string

Code: Select all

if (comando == "quit") ...

Code: Select all

if (!comando.compare("quit")) ...
Thanks
mathmoi
Posts: 290
Joined: Mon Mar 13, 2006 5:23 pm
Location: Québec
Full name: Mathieu Pagé

Re: Question about string comparison

Post by mathmoi »

P. Villanueva wrote:Please could anyone explain me the difference between these two codes?

Note: comando is a string

Code: Select all

if (comando == "quit") ...

Code: Select all

if (!comando.compare("quit")) ...
Thanks
I'll assume your code is C#.

The first example compare two string and return true if they are the same.

The second example compare tow string and return a numerical value indicating if the first is greater or they are the same or the second is greater (http://msdn.microsoft.com/en-us/library ... S.71).aspx). Then the resulting value is transformed into a boolean and inversed.

In practice both example are equivalent, however, the second will be slower because it imply more work to get to the same result. Moreover, I personally find the second example less clear.
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: Question about string comparison

Post by Sven »

mathmoi wrote:
P. Villanueva wrote:Please could anyone explain me the difference between these two codes?

Note: comando is a string

Code: Select all

if (comando == "quit") ...

Code: Select all

if (!comando.compare("quit")) ...
Thanks
I'll assume your code is C#.

The first example compare two string and return true if they are the same.

The second example compare tow string and return a numerical value indicating if the first is greater or they are the same or the second is greater (http://msdn.microsoft.com/en-us/library ... S.71).aspx). Then the resulting value is transformed into a boolean and inversed.

In practice both example are equivalent, however, the second will be slower because it imply more work to get to the same result. Moreover, I personally find the second example less clear.
I agree that the first example is much more clear. But it is not necessarily faster. Maybe both are equally fast, if they are both based on the "strcmp" algorithm which in fact does the same as you described for "compare". So if == is implemented as strcmp(x, y) == 0 then both are in fact nearly identical.

One more nitpicking may be allowed (maybe I am wrong here w.r.t. C#):
Then the resulting value is transformed into a boolean and inversed.
While I don't know how this works in C#, in C and C++ it is different: the resulting value, a signed integer, is converted to an integer from {0, 1} by applying the ! operator (nonzero into 0, zero into 1), and becomes a boolean only by being used within an if-expression.

The result is of course the same ...

Sven
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: Question about string comparison

Post by bob »

P. Villanueva wrote:Please could anyone explain me the difference between these two codes?

Note: comando is a string

Code: Select all

if (comando == "quit") ...

Code: Select all

if (!comando.compare("quit")) ...
Thanks
I am assuming C/C++, not sure if that is correct or not. If so, then...

"commando" is a pointer to an array of characters. "quit" is _also_ a pointer to an array of characters. You are asking "are the addresses of these two strings the same?" and the answer will always be "no."

strcmp() understands this and compares the two arrays of characters themselves, not their addresses.
Last edited by bob on Wed Jan 13, 2010 5:25 pm, edited 1 time in total.
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: Question about string comparison

Post by bob »

Sven Schüle wrote:
mathmoi wrote:
P. Villanueva wrote:Please could anyone explain me the difference between these two codes?

Note: comando is a string

Code: Select all

if (comando == "quit") ...

Code: Select all

if (!comando.compare("quit")) ...
Thanks
I'll assume your code is C#.

The first example compare two string and return true if they are the same.

The second example compare tow string and return a numerical value indicating if the first is greater or they are the same or the second is greater (http://msdn.microsoft.com/en-us/library ... S.71).aspx). Then the resulting value is transformed into a boolean and inversed.

In practice both example are equivalent, however, the second will be slower because it imply more work to get to the same result. Moreover, I personally find the second example less clear.
I agree that the first example is much more clear. But it is not necessarily faster. Maybe both are equally fast, if they are both based on the "strcmp" algorithm which in fact does the same as you described for "compare". So if == is implemented as strcmp(x, y) == 0 then both are in fact nearly identical.

One more nitpicking may be allowed (maybe I am wrong here w.r.t. C#):
Then the resulting value is transformed into a boolean and inversed.
While I don't know how this works in C#, in C and C++ it is different: the resulting value, a signed integer, is converted to an integer from {0, 1} by applying the ! operator (nonzero into 0, zero into 1), and becomes a boolean only by being used within an if-expression.

The result is of course the same ...

Sven
It also isn't clear the first is correct. Could also be C/C++ code as well.
mathmoi
Posts: 290
Joined: Mon Mar 13, 2006 5:23 pm
Location: Québec
Full name: Mathieu Pagé

Re: Question about string comparison

Post by mathmoi »

Sven Schüle wrote: I agree that the first example is much more clear. But it is not necessarily faster. Maybe both are equally fast, if they are both based on the "strcmp" algorithm which in fact does the same as you described for "compare". So if == is implemented as strcmp(x, y) == 0 then both are in fact nearly identical.
I admit I don't know how it's implemented, however it seems it would be a little bit more faster to simply check for equality than to do what strcmp do (Check for equality, then check . So I assume that's what the compiler does. But that's just an assumption.
P. Villanueva
Posts: 85
Joined: Sat May 17, 2008 10:57 pm
Location: Bilbao, Spain

Re: Question about string comparison

Post by P. Villanueva »

Thanks for the replies.

Its C++ code.

The story is that I used the "==" operator in KMT Chess and it worked fine (I use old Dev-C++ IDE with mingw32 as compiler).
But when Dann Corbit built the 64bit binary, he moved it to ".compare()". I didn´t understand why he did such think.
"commando" is a pointer to an array of characters. "quit" is _also_ a pointer to an array of characters. You are asking "are the addresses of these two strings the same?" and the answer will always be "no."
But the binary I built using "==" operator worked. Do different compilers have different idea about what "==" operator is?
User avatar
Bo Persson
Posts: 260
Joined: Sat Mar 11, 2006 8:31 am
Location: Malmö, Sweden
Full name: Bo Persson

Re: Question about string comparison

Post by Bo Persson »

P. Villanueva wrote:Thanks for the replies.

Its C++ code.

The story is that I used the "==" operator in KMT Chess and it worked fine (I use old Dev-C++ IDE with mingw32 as compiler).
But when Dann Corbit built the 64bit binary, he moved it to ".compare()". I didn´t understand why he did such think.
"commando" is a pointer to an array of characters. "quit" is _also_ a pointer to an array of characters. You are asking "are the addresses of these two strings the same?" and the answer will always be "no."
But the binary I built using "==" operator worked. Do different compilers have different idea about what "==" operator is?
No, but different languages do. In C++ you have a std::string class with both a compare member and an operator== defined for it.

Unlike array of char, std::string will compare the content of the strings and not their addresses.

On my compiler, the string operator==(_Left, _Right) is implemented as

{ return _Left.compare(_Right) == 0; }


Now guess which will be fastest! :-)
MattieShoes
Posts: 718
Joined: Fri Mar 20, 2009 8:59 pm

Re: Question about string comparison

Post by MattieShoes »

In case anybody wants to know...
In C#, there isn't a "Compare" method for strings. There is a CompareTo method though.
Also, it doesn't like casting ints to bools, even explicitly

Code: Select all

command.Equals("quit") //works
command.CompareTo("quit")==0 //works
command == "quit" //works

!command.CompareTo("quit") //fails, using ! on int
!((bool)command.CompareTo("quit")) //fails, casting int to bool

//You can of course string together methods
if (command.Trim().ToLower().Equals("quit"))
    ...