donpal
donpal

Reputation: 2112

trimming strings with actionscript 3

I'm trying to trim white spaces from front and end of a string. Looks like as3 does not have a trim function. Anyone aware of how it can be done?

Upvotes: 15

Views: 25949

Answers (7)

Reado
Reado

Reputation: 1452

Try this

str = StringUtil.trim(str);

Upvotes: 2

fseydel
fseydel

Reputation: 175

ItzWarty's solution does not remove trailing spaces.

Amargosh's solution does work perfectly for me, and is also given at http://jeffchannell.com/ActionScript-3/as3-trim.html.

Unfortunatly I don't have enough reputation to vote up Amargosh's solution.

Additionally I had to remove doublequotes so here's my trim:

function trim( s:String ):String
{
  return s.replace(/^[\s|"]+|[\s|"]+$/gs, '');
}

Upvotes: 1

bug-a-lot
bug-a-lot

Reputation: 2454

You have a method that's called trim in the utility class called StringUtil => http://livedocs.adobe.com/flex/3/langref/mx/utils/StringUtil.html#trim%28%29

Upvotes: 24

Amarghosh
Amarghosh

Reputation: 59451

str = str.replace(/^\s+|\s+$/g, '');

Upvotes: 6

phwd
phwd

Reputation: 19995

Did you check Adobe's Documentation ? http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/String.html

package {
import flash.display.Sprite;

public class StringExample extends Sprite {
    public function StringExample() {
        var companyStr:String = new String("     Company X");
        var productStr:String = "Product Z Basic     ";
        var emptyStr:String = " ";
        var strHelper:StringHelper = new StringHelper();

        var companyProductStr:String = companyStr + emptyStr + productStr;
        trace("'" + companyProductStr + "'");    // '     Company X Product Z Basic     '

        companyProductStr = strHelper.replace(companyProductStr, "Basic", "Professional");
        trace("'" + companyProductStr + "'");    // '     Company X Product Z Professional     '

        companyProductStr = strHelper.trim(companyProductStr, emptyStr);
        trace("'" + companyProductStr + "'");    // 'Company X Product Z Professional'
    }
}

}

class StringHelper {

public function StringHelper() {
}

public function replace(str:String, oldSubStr:String, newSubStr:String):String {
    return str.split(oldSubStr).join(newSubStr);
}

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;
}

public function stringToCharacter(str:String):String {
    if (str.length == 1) {
        return str;
    }
    return str.slice(0, 1);
}

}

UPDATE : Oh just saw ItzWarty . -.-

Upvotes: 3

quoo
quoo

Reputation: 6307

You should be able to use regex, something like:

var pattern:RegExp = /(\t|\n|\s{2,})/g;  
trimmedString = untrimmedString.replace(pattern, '');  

Upvotes: 1

Warty
Warty

Reputation: 7395

Look at http://jeffchannell.com/ActionScript-3/as3-trim.html

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

http://www.designscripting.com/2008/11/string-utils-in-as3/ has a ton of string utility functions, including string trim

Upvotes: 2

Related Questions