Category Archives: string manipulation

Object from String

Actionscript:
  1. var str:String='{x: 10, y:10, width:100, heigth:100, name:"myClip", type:"clip"}';
  2.  
  3. var obj:Object=toObject(str);
  4.  
  5. trace(obj.x + obj.width, obj.name, obj.type);
  6.  
  7. function toObject(str:String):Object {
  8.     str=str.replace(/\{|\}/g,"");
  9.     // to an array of name value pairs [0] = name, [1] = value etc...
  10.     var pairs:Array=str.split(/\:|\,\s+/);
  11.     var obj:Object = new Object();
  12.     for (var i:int = 0; i<pairs.length; i+=2) {
  13.         obj[pairs[i]]=numOrString(pairs[i+1]);
  14.     }
  15.     return obj;
  16. }
  17. function numOrString(val:*):* {
  18.     if (isNaN(Number(val))) {
  19.         if (val.charAt(0) == '"' && val.charAt(val.length - 1) == '"'){
  20.             val = val.substr(1, val.length - 2);
  21.         }
  22.     } else {
  23.         val=Number(val);
  24.     }
  25.     return val;
  26. }

A very limited way to parse an object from a string... Tomorrow I'll post a better version of this that's a good deal more complex and doesn't really use regular expressions... This works nicely if you don't have arrays as Object properties...

Also posted in dynamic, strings | Tagged , | 2 Comments

Formating Numbers with Commas

Actionscript:
  1. trace(formatNum(1000));
  2. trace(formatNum(10000000));
  3. trace(formatNum(1000000.39485));
  4.  
  5. function formatNum(num:Number):String {
  6.     var newStr:String = "";
  7.     var str:String = num.toString();
  8.    
  9.     var parts:Array = str.split(".");
  10.     str = parts[0];
  11.     var end:String = (parts[1]) ? "." + parts[1] : "";
  12.    
  13.     var i:int = str.length;
  14.     while(i--> 0){
  15.          var char:String = str.charAt(i);
  16.          if ((str.length - i) % 3 == 0){
  17.             newStr = "," + char +newStr;
  18.          }else{
  19.             newStr = char +  newStr;
  20.          }
  21.     }
  22.     return newStr + end;
  23. }
  24. /*
  25. outputs:
  26. 1,000
  27. 10,000,000
  28. 1,000,000.39485
  29. */

The function formatNum() returns a string given a number argument ... it adds commas where appropriate and takes into account decimal values. This is useful anywhere you need to display numbers over 1,000, in a game, in a calculator app etc...

Posted in string manipulation | 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 strings | 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, strings | Leave a comment