Category Archives: strings

String.localeCompare()

Actionscript:
  1. var letterA:String = "a";
  2. var letterZ:String = "z";
  3.  
  4. trace(letterA.localeCompare(letterZ));
  5. trace(letterZ.localeCompare(letterA));
  6. /*
  7. outputs
  8. -25
  9. 25
  10. */

String.localeCompare() calculates the sorting order between two Strings....

Posted in strings | Tagged , | Leave a comment

String.indexOf()

Actionscript:
  1. var words:String = "one two three four five";
  2.  
  3. trace("two: ", words.indexOf("two"));
  4.  
  5. var letters:String = "abcd";
  6.  
  7. trace("d: ",letters.indexOf("d"));
  8.  
  9. trace("z: ",letters.indexOf("z"));
  10.  
  11. /*
  12. outputs:
  13. two:  4
  14. d:  3
  15. z:  -1
  16. */

indexOf() searches a string for another string and returns an index... in line 3 above, I search the words string for the smaller string "two" and indexOf() gives me the index of the letter "t". If indexOf() doesn't find anything it will return -1 (as in the case of line 9).

I seem to recall using this in some unexpected places. I'll see if I can dig up an example over the next few days.

Also posted in string manipulation | Tagged , | Leave a comment

Zig Zag Binary ix2 & i/2

Actionscript:
  1. var txt:TextField = TextField(addChild(new TextField()));
  2. txt.text = "";
  3. txt.width = 190;
  4. txt.height = 400;
  5. txt.multiline = true;
  6.  
  7. var count:int = 1;
  8. function render():void{
  9.     var line = int(count).toString(2);
  10.     while(line.length <31){
  11.         line = "0" + line;
  12.     }
  13.     txt.appendText(line + "\n");
  14.     txt.scrollV= txt.maxScrollV;
  15. }
  16.  
  17. addEventListener(Event.ENTER_FRAME, onCountUp);
  18. function onCountUp(evt:Event):void {
  19.     count *= 2;
  20.     render();
  21.     if (count ==0x40000000){
  22.         removeEventListener(Event.ENTER_FRAME, onCountUp);
  23.         addEventListener(Event.ENTER_FRAME, onCountDown);
  24.     }
  25. }
  26. function onCountDown(evt:Event):void {
  27.     count /= 2;
  28.     render();
  29.     if (count ==1){
  30.         addEventListener(Event.ENTER_FRAME, onCountUp);
  31.         removeEventListener(Event.ENTER_FRAME, onCountDown);
  32.     }
  33. }

The above animates a zig zag pattern in a TextField.

Also posted in Math, string manipulation | Leave a comment

MYSQL SOUNDEX

C:
  1. SELECT * FROM _users WHERE SOUNDEX(name) LIKE SOUNDEX('jon');

Not ActionScript, but the coolest thing I've seen in Mysql in awhile, the soundex function will convert a string into a soundex index... read more about it on wikipedia.

I did some tests and it matches things like:

what's your name? & whats yer name

very cool... since this isn't ActionScript, I will post another snippet right after this.

UPDATE: Actually started reading more about this stuff and dug up some algorithms... metaphone etc... fun stuff, maybe I'll port some of it to actionscript...

Also posted in misc, string manipulation | Leave a comment