7 Trim() Equivalent Function In Flash Might Help Someone Else

audium-icon In PHP we have a function called Trim() to remove characters from a string. However there are no the equivalent to PHP’s trim in Flash. But most time we need to trim a string with extra white space, so we can do only write this by ourselves. The following is 7 actionscript function which can do the same. They might help someone else. *If you know more please don’t hesitate to leave a comment.

1.

  1. class Toolkit {
  2.     public static function trim(str:String):String {
  3.         var char:String;
  4.         while ((char = str.charAt(0)) == chr(Key.SPACE) || char == chr(Key.TAB))
  5.             str = str.substr(1);
  6.         while ((char = str.charAt(str.length1)) == chr(Key.SPACE) || char == chr(Key.TAB))
  7.             str = str.substr(0, str.length1);
  8.         return str;
  9.     }
  10. }

2.

  1. function trim(string) {
  2. if (string.indexOf(" ") != -1) {
  3. whiteIndex = string.indexOf(" ");
  4. fst = string.slice(0, whiteIndex);
  5. snd = string.slice(whiteIndex+1, string.length);
  6. trim((fst+snd));
  7. } else {
  8. _root.noSpaces = string;
  9. }
  10. }

3.

  1. function trim(str:String) :String {
  2. if(str=="") return "";
  3. var newStr:String;
  4. var len:Number = str.length;
  5. var s:Number = 0;
  6. for(var i=0; i<len ; ++i) {
  7. if(str.substr(i,1)==" ") s++;
  8. else break;
  9. }
  10. newStr = str.slice(s);
  11. s = 0;
  12. for(var i=(len-1); i>-1; –i) {
  13. if(str.substr(i,1)==" ") s–;
  14. else break;
  15. }
  16. s = s==0? len : s;
  17. newStr = newStr.slice(0,s);
  18. return newStr;
  19. }

4.

  1. public function trim(str:String, char:String):String {
  2.     return trimBack(trimFront(str, char), char);
  3. }
  4.  
  5. public function trimFront(str:String, char:String):String {
  6.     char = stringToCharacter(char);
  7.     if (str.charAt(0) == char) {
  8.         str = trimFront(str.substring(1), char);
  9.     }
  10.     return str;
  11. }
  12.  
  13. public function trimBack(str:String, char:String):String {
  14.     char = stringToCharacter(char);
  15.     if (str.charAt(str.length - 1) == char) {
  16.         str = trimBack(str.substring(0, str.length - 1), char);
  17.     }
  18.     return str;
  19. }

5.

  1. // parameters:
  2. // matter, string to be trimmed
  3. // returns:
  4. // string, whitespaces removed from left side.
  5. function ltrim(matter) {
  6.     if ((matter.length>1) || (matter.length == 1 && matter.charCodeAt(0)>32 && matter.charCodeAt(0)<255)) {
  7.         i = 0;
  8.  
  9.         while (i<matter.length && (matter.charCodeAt(i)<=32 || matter.charCodeAt(i)>=255)) {
  10.             i++;
  11.         }
  12.  
  13.         matter = matter.substring(i);
  14.     } else {
  15.         matter = "";
  16.     }
  17.     return matter;
  18. }
  19.  
  20. // parameters:
  21. // matter, string to be trimmed
  22. // returns:
  23. // string, whitespaces removed from right side.
  24. function rtrim(matter) {
  25.     if ((matter.length>1) || (matter.length == 1 && matter.charCodeAt(0)>32 && matter.charCodeAt(0)<255)) {
  26.         i = matter.length-1;
  27.  
  28.        while (i>=0 && (matter.charCodeAt(i)<=32 || matter.charCodeAt(i)>=255)) {
  29.             i–;
  30.         }
  31.         matter = matter.substring(0, i+1);
  32.     } else {
  33.         matter = "";
  34.     }
  35.  
  36.     return matter;
  37. }
  38.  
  39. // parameters:
  40. // matter, string to be trimmed
  41. // returns:
  42. // string, whitespaces removed from both sides.
  43.  
  44. function trim(matter) {
  45.     return ltrim(rtrim(matter));
  46. }

6.

  1. function LTrim(s:String):String {
  2. var i=0;
  3. while ((i<s.length) && (s.substr(i,1)==’ ‘)) i++;
  4. return (s.substr(i,s.length-i));
  5. }
  6.  
  7. function RTrim(s:String):String {
  8. var i=s.length -1;
  9.  
  10. while ((i>0) && (s.substr(i,1)==’ ‘)) i–;
  11.  
  12. return (s.substr(0,i+1));
  13. }
  14.  
  15. function Trim(s:String) {
  16. return(LTrim(RTrim(s)));
  17. }

7.

  1. function trim( s:String ):String
  2. {
  3.   return s.replace( /^([\s|\t|\n]+)?(.*)([\s|\t|\n]+)?$/gm, "$2" );
  4. }
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • Technorati
  • StumbleUpon
  • Twitter
RSS Enjoy this Post? Subscribe to Ntt.cc

RSS Feed   RSS Feed     Email Feed  Email Feed Follow us Follow us
You can leave a response, or trackback from your own site.

4 Responses to “7 Trim() Equivalent Function In Flash Might Help Someone Else”

  1. Andy says:

    What about just using StringUtil.trim()? It trims whitespace from the beginning and end of the string, and it’s build in. I just found it and used it this morning.

  2. Janes says:

    Package mx.utils
    Class public class StringUtil
    Inheritance StringUtil Inheritance Object

    Language Version: ActionScript 3.0
    Product Version: Flex 3
    Runtime Versions: Flash Player 9, AIR 1.1

  3. Hereisme says:

    how can i get a list of all child’s in a flex application ?

  4. [...] 原文:7 Trim 也曾为Trim纠结过。很久没浏览订阅了,今儿看到了。贴上。 1. [...]

Leave a Reply