Forum Discussion

Simon_InT's avatar
Simon_InT
Contributor
7 years ago
Solved

aqString.Compare

I am using the aqString.Compare and it's returning the incorrect result. The code looks like this:

 

var String1 = "15";
var String2 = "7"
var test = aqString.Compare(String1, String2, false)

 

test gets evaluated to -1, meaning String1 is less than String2. Clearly 15 is not less than 7, so I don't understand.

 

Has anyone else observed this? Is there something I'm doing wrong?

  • As baxatob pointed out,, aqString.Compare does a character comparison of strings.  If you're looking at comparing numeric values to determine if one is less than or greater than another, I would suggest converting the strings to integers and then comparing.

     

    var String1 = "15";
    var String2 = "7";
    
    if (aqConvert.StrToInt(String1) < aqConvert.StrToInt(String2)) {
        Log.Message('Well, this didn't work');
    }
    else { Log.Message('Worked just fine')}

3 Replies

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    As baxatob pointed out,, aqString.Compare does a character comparison of strings.  If you're looking at comparing numeric values to determine if one is less than or greater than another, I would suggest converting the strings to integers and then comparing.

     

    var String1 = "15";
    var String2 = "7";
    
    if (aqConvert.StrToInt(String1) < aqConvert.StrToInt(String2)) {
        Log.Message('Well, this didn't work');
    }
    else { Log.Message('Worked just fine')}
  • baxatob's avatar
    baxatob
    Community Hero

    According to documentation aqString.Compare() method uses character codes (ASCII) and performing symbol-by-symbol and finishes once a difference is found.

    In your case it finds the difference at first symbol, decides that 1 < 7 - and returns -1 as a result. 

    Try to change String1 = "85" and it returns 1. 

     

    If you need to compare the length of the strings, you can use aqString.GetLength() method.