AS3 Remove Non-Numeric Characters from a String


/ Published in: ActionScript 3
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function removeNonNumericChars($str:String):String {
  2. var newStr:String = "";
  3. for (var i:int = 0; i<$str.length; i++) {
  4. var currentCharCode:Number = $str.charCodeAt(i);
  5. if ((currentCharCode >= 48) && (currentCharCode <= 57)) {
  6. newStr += $str.charAt(i);
  7. }
  8. }
  9. return newStr;
  10. }
  11.  
  12. var myString:String = "#12abc345^67%89)(-@!foo";
  13. trace("myString: "+myString);
  14. myString = removeNonNumericChars(myString);
  15. trace("myString: "+myString);
  16.  
  17. // OUTPUT
  18. // myString: #12abc345^67%89)(-@!foo
  19. // myString: 123456789

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.