AS3 :: StringUtils Class

Came up with this idea after perusing proto.layer51.com. It's basically a sub-class of String and the 1st method is a reduction method that let's you feed in a string, a limit and a "there's more" indicator. Anyways, here's the code:

class labs.otuome.utils.StringUtils extends String
{
	public var theString:String;
 
	public function StringUtils(string:String)
        {
		super();
		theString = string;
	}
 
	public function reduce(stringLength,trimIndicator):String
	{
		var trimmedString:Array = [];
		if (theString.length < = stringLength) 
                {
			//just return the string if it's within the "limit"
			return theString;
		}
 
                var words:Array = theString.split(" ");
                var numWords:Number = words.length;
                var ol:Number = 0;
                var cWord:String;
                var w:Number;
 
                for (w=0; w<numWords; ++w)
                {
                   cWord=words[w];
                   var cwl=cWord.length;
                   //check each words length before adding it the output string
                   if ((ol+cwl)<=stringLength)
                   {
                      trimmedString.push(cWord);
                      ol+=cwl+1;
                   }
                   else break;
                }
                return trimmedString.join(" ")+trimIndicator;
	}
}

Copyright © 2008 Otuome Labs. All rights reserved.

sfy39587f11