Ever thought of creating a teaser text of some longer string in Flash? For programmers still coding in ActionScript 2, here’s the piece of code to do it.

String.prototype.tease = function(strlen):String {
	// split ln 1
	var line1:String = this.split(String.fromCharCode(13),1).toString();
	// see if we need to cut at all
    if (line1.length > strlen) {
		// split 1st line on spaces
        var words:Array = line1.split(" ");
        // split on word if the string's 1st word is too long
        if (words[0].length > strlen) {
            return this.substr(0, strlen) + "...";
        } else {
            var teaser:String = words[0];
            for (wordNo=1; words[wordNo]; wordNo++) {
                var curStrLength:Number = teaser.length + words[wordNo].length;
                if (curStrLength > strlen) {
                    return teaser + "...";
                } else {
                    teaser += " " + words[wordNo];
                }
            }
        }
    } else {
        return line1;
    }
};
ASSetPropFlags(String.prototype, "tease", 1);

Download the above script and the test case.

Share and bookmark:

  • Twitter
  • Technorati
  • StumbleUpon
  • Digg
  • Google Bookmarks
  • Facebook
  • MySpace
  • del.icio.us
  • Mixx
  • blogmarks
  • Live
  • NewsVine
  • Reddit
  • Slashdot
  • LinkedIn
  • FriendFeed
  • email