Game Development Community

Question re: string comparisons

by Kevin Rogers · in Torque Game Engine · 08/05/2006 (4:10 pm) · 6 replies

Kind of a newb question, but it's been nagging me for a while, so...

Since there are direct string comparison operators, $= and !$=, why would I ever need to use stricmp()?

#1
08/08/2006 (7:59 am)
From TDN
Quote:
stricmp( string1 , string2 )

Purpose
Use the stricmp function to do a lexicographic case in-sensitive string comparison between string1 and string2.
function strcmptest() 
{
  echo("Lexicographic comparisons are not the same as arithmetic comparisons...");
  
  echo("100 - 10 == 90, but strcmp( \"100\" , \"10\" ) == " , 
       strcmp( "100" , "10" ) );
        
  echo("\n", "Don't forget about case-senstivity...");
 
  echo("strcmp( \"ABC\" , \"abc\" )  == " , 
       strcmp( "ABC" , "abc" ) , "\n\n, but \n" );
  
  echo("stricmp( \"ABC\" , \"abc\" ) == " , 
       stricmp( "ABC" , "abc" ) );
}


strcmptest();

Lexicographic comparisons are not the same as arithmetic comparisons...
100 - 10 == 90, but strcmp( "100" , "10" ) == 1

Don't forget about case-senstivity...
strcmp( "ABC" , "abc" )  == -1

, but 

stricmp( "ABC" , "abc" ) == 0
#2
08/08/2006 (8:12 am)
@kevin - i've wondered the same thing.

as far as i can tell, [$a $= $b] = [!stricmp($a, $b)].

i suspect it may be just for completeness in the str...() function set.
ie if you're writing a bunch of code in the mode of strcmp, strlen, etc, it would be awkward/inelegant to switch modes to $= just for case-insensitive compare.
#3
08/08/2006 (10:24 am)
I think $= IS case sensitive and it's stricmp that isn't.
#4
08/08/2006 (10:31 am)
$= is not case sensitive.
#5
08/08/2006 (10:34 am)
There ya go. heh. Shows how much string work I've done.
#6
08/08/2006 (1:01 pm)
Thanks for the input guys!

@Anthony: Sorry, but that didn't clear up anything for me... Should I be comparing numbers with $= ? I'm dubious. And what the heck does "lexicographic" mean in this context anyway? =P

@Orion: I was looking at it from the standpoint, $= is so much simpler to use and read, why would I clutter up my code with stricmp(). But that makes perfect sense.

@Cliff: It makes more sense to me too that $= would be a case sensitive compare (i.e. is $a _exactly_ equal to $b) and then you'd use stricmp() if you wanted to get more specific. But I suppose GG had a good reason for doing it this way...

What we need is a case sensitive operator, then we can ignore the strcmp()s altogether. =)