How to alphabetically compare two lines in ActionScript 3

I used strcmp (x, y) in C ++. Do you know how to do this in as3?

Thanks!

+4
source share
4 answers

You can use regular operands! =! == == <> <= =>

+6
source

If this is a simple string comparison you need, don't worry about writing it yourself.

var result:int = ObjectUtil.compare("stringA","stringB"); 

This provides what you are looking for.

+8
source

For a complete comparison of Unicode strings, you should use the localeCompare method:

var compare:int = a.localeCompare(b);

It will return the alphabetical difference between the two first two letters (whether the other letter is the first or any other) or zero if the strings are identical. The number will be negative if "a" is the first in the alphabet, or positive if it is "b" first.

So you will need to check:

compare < 0 (first appears "a")

compare == 0 (identical lines)

compare > 0 ("b" first appears)

You must also make sure that both a and b have lowercase letters in advance (or both are uppercase, it doesn’t matter, but both should be in the same case), because localeCompare thinks that uppercase and lowercase letters are completely different alphabets (this is because this method compares the Unicode character table, the uppercase letter begins).

+3
source

You will need to implement this function yourself. This is just an example, it will certainly take a little more work than this ...

  private function strComp( string1:String , string2:String ):int { var counter:int; for( var i:int ; i < string1.length ; ++i ) { if( string1.getCharAt(i) == string2.getCharAt(i) ) //increment counter else break; } return counter; } 
0
source

All Articles