7 Trim() Equivalent Function In Flash Might Help Someone Else - Ntt.cc

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

audium-iconIn 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.

class Toolkit {
    public static function trim(str:String):String {
        var char:String;
        while ((char = str.charAt(0)) == chr(Key.SPACE) || char == chr(Key.TAB))
            str = str.substr(1);
        while ((char = str.charAt(str.length – 1)) == chr(Key.SPACE) || char == chr(Key.TAB))
            str = str.substr(0, str.length – 1);
        return str;
    }
}

2.

function trim(string) {
if (string.indexOf(" ") != -1) {
whiteIndex = string.indexOf(" ");
fst = string.slice(0, whiteIndex);
snd = string.slice(whiteIndex+1, string.length);
trim((fst+snd));
} else {
_root.noSpaces = string;
}
}

3.

function trim(str:String) :String {
if(str=="") return "";
var newStr:String;
var len:Number = str.length;
var s:Number = 0;
for(var i=0; i<len ; ++i) {
if(str.substr(i,1)==" ") s++;
else break;
}
newStr = str.slice(s);
s = 0;
for(var i=(len-1); i>-1; –i) {
if(str.substr(i,1)==" ") s–;
else break;
}
s = s==0? len : s;
newStr = newStr.slice(0,s);
return newStr;
}

4.

public function trim(str:String, char:String):String {
    return trimBack(trimFront(str, char), char);
}

public function trimFront(str:String, char:String):String {
    char = stringToCharacter(char);
    if (str.charAt(0) == char) {
        str = trimFront(str.substring(1), char);
    }
    return str;
}

public function trimBack(str:String, char:String):String {
    char = stringToCharacter(char);
    if (str.charAt(str.length - 1) == char) {
        str = trimBack(str.substring(0, str.length - 1), char);
    }
    return str;
}

5.

// parameters:
// matter, string to be trimmed
// returns:
// string, whitespaces removed from left side.
function ltrim(matter) {
    if ((matter.length>1) || (matter.length == 1 && matter.charCodeAt(0)>32 && matter.charCodeAt(0)<255)) {
        i = 0;

        while (i<matter.length && (matter.charCodeAt(i)<=32 || matter.charCodeAt(i)>=255)) {
            i++;
        }

        matter = matter.substring(i);
    } else {
        matter = "";
    }
    return matter;
}

// parameters:
// matter, string to be trimmed
// returns:
// string, whitespaces removed from right side.
function rtrim(matter) {
    if ((matter.length>1) || (matter.length == 1 && matter.charCodeAt(0)>32 && matter.charCodeAt(0)<255)) {
        i = matter.length-1;

       while (i>=0 && (matter.charCodeAt(i)<=32 || matter.charCodeAt(i)>=255)) {
            i–;
        }
        matter = matter.substring(0, i+1);
    } else {
        matter = "";
    }

    return matter;
}

// parameters:
// matter, string to be trimmed
// returns:
// string, whitespaces removed from both sides.

function trim(matter) {
    return ltrim(rtrim(matter));
}

6.

function LTrim(s:String):String {
var i=0;
while ((i<s.length) && (s.substr(i,1)==’ ‘)) i++;
return (s.substr(i,s.length-i));
}

function RTrim(s:String):String {
var i=s.length -1;

while ((i>0) && (s.substr(i,1)==’ ‘)) i–;

return (s.substr(0,i+1));
}

function Trim(s:String) {
return(LTrim(RTrim(s)));
}

7.

function trim( s:String ):String
{
  return s.replace( /^([\s|\t|\n]+)?(.*)([\s|\t|\n]+)?$/gm, "$2" );
}
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.

5 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. [...]

  5. [...] the articles which were published before, 7 Trim() Equivalent Function In Flash Might Help Someone Else, 16 Useful Mathematical Formulas In ActionScript 3 and 12 very simple,basic but useful function [...]

Leave a Reply