I have RTRIM; how to make LTRIM one?
public static function rtrim(string:String):String
{
return string.replace(/\s+$/,"");
}
I have RTRIM; how to make LTRIM one?
public static function rtrim(string:String):String
{
return string.replace(/\s+$/,"");
}
public static function ltrim(string:String):String {
return string.replace(/^\s+/,"");
}
Caveat: Untested! Look up the flex 3.0 documentation here. This is exactly similar to what you have, except that we use a different metacharacter to specify that we want to start searching for whitespaces (\s
-- another metacharacter) from the begining(^
) instead of from the end($
). The +
after \s
tells the pattern matches to grok one or more whitespaces.
Wow, seriously? You're using regular expressions to remove a constant sequence of characters from the ends of a string? Really? I don't know Actionscript/Flex, but this isn't the way to go. After a quick google I found a solution which may or may not be more efficient.
Instead of re-inventing the wheel, why not just use the StringUtil class from Adobe's as3corelib library?
Out of interest, as3corelib defines it's trim functions as follows:
public static function trim(input:String):String
{
return StringUtil.ltrim(StringUtil.rtrim(input));
}
public static function ltrim(input:String):String
{
var size:Number = input.length;
for(var i:Number = 0; i < size; i++)
{
if(input.charCodeAt(i) > 32)
{
return input.substring(i);
}
}
return "";
}
public static function rtrim(input:String):String
{
var size:Number = input.length;
for(var i:Number = size; i > 0; i--)
{
if(input.charCodeAt(i - 1) > 32)
{
return input.substring(0, i);
}
}
return "";
}