Posted in Adobe, Flash, Flex
10/5 2006

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 org.thesourcecode.utils.StringUtils extends String
{
	public var theString:String;
 
	public function StringUtils( string:String )
	{
		super();
		theString = string;
	}
 
	public function reduce( stringLength:Number, trimIndicator:String="..." ):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;
        }
}

The easy-install extension can be downloaded here.
Documentation can be found here.

This Post tags: , ,

 

USER COMMENTS

Track comments via RSS 2.0 feed. Feel free to post the comment, or trackback from your web site.

Currently there are no comments related to article "StringUtils Class".

Get Adobe Flash playerPlugin by wpburn.com wordpress themes