Posted By

adrianparr on 05/07/10


Tagged

number format substr space String mobile join split as3 spaces


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

tspitzr


AS3 Format a UK Mobile Number (XXXXX XXXXXX)


 / Published in: ActionScript 3
 

This returns a string with a single space after the fifth character. Useful for displaying a UK mobile number which is easy to read.

  1. function formattedMobileNum($numStr:String):String {
  2. var withNoSpaces:String = $numStr.split(" ").join("");
  3. var part1:String = withNoSpaces.substr(0, 5);
  4. var part2:String = withNoSpaces.substr(5, withNoSpaces.length-1);
  5. var formatted = part1 + " " + part2;
  6. formatted = formatted.replace(/^\s+|\s+$/g, "");
  7. return formatted;
  8. }
  9.  
  10. var mobileNum:String = " 0 785 41 23 456 ";
  11. trace(formattedMobileNum(mobileNum));
  12. // OUTPUT
  13. // 07854 123456

Report this snippet  

You need to login to post a comment.