WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Strings

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

StrCmp Problem

 Keywords: StrCmp StriCmp Compare strings numbers integers 

Question:

I am using strCmp and the results are strange.
test=StrCmp("20","3")
Message("",test)
Shouldn't this return a 1 since string1 is greater than string2? But it always returns a -1. Why is this simple code returning thees results?

Answer:

You are confusing string comparison with numeric comparison. The StrCmp function compares characters in a string until it finds a difference. It then reports the relational position of the two differing characters relative to each other in the collating sequence. In this case it finds a difference at the first character and reports the relational difference between the character '2' and character '3' in the collating sequence. Since '2' is less than '3' in the collating sequence it is correctly returning -1.

If you want to compare numbers I recommend using the relational operators instead.

num1 = 20
num2 = 3
If num1 == num2
     Pause("","Num1 is Equal to Num2")
EndIf
If num1 < num2
     Pause("","Num1 is Less Than Num2")
EndIf
If num1 > num2
     Pause("","Num1 is Greater Than Num2")
EndIf

Article ID:   W18289
Filename:   StrCmp Problem.txt
File Created: 2008:12:22:09:46:50
Last Updated: 2008:12:22:09:46:50