You pass this method of the String object, a length to cut the string down to, and a pattern that you want to be returned at the end of the string:
this.onEnterFrame=function()
String.prototype.reduce=function(l,p)
{
if(this.length<=l) return this;
var words=this.split(" ")
var numWords=words.length
var output=[]
var ol,cWord,w
for(w=0;w
cWord=words[w]
cwl=cWord.length
if((ol+cwl)<=l)
{
output.push(cWord)
ol+=cwl+1
}
else break
}
return output.join(" ")+(this.length>l) ? p
}
And here is how you would use this method:
rhyme="Bah bah, black sheep, have you any wool?"
cutRhyme=rhyme.reduce(22,"...")
For examples sake:
trace(cutRhyme);
Outputs:
this.onEnterFrame=function()
Bah bah black sheep...
discuss this topic to forum
