You are here: Home > Programming > Flash programming > Code repository > Useful code > Text truncation
This code truncates a string of text by counting the maximum number of characters, and replacing the rest with 3 dots.
String.prototype.reduce = function(l, p) {
var words = this.split(" ");
var wordCount = words.length;
var output = [];
var ol, cWord, w;
for (w = 0; w < wordCount; ++w) {
cWord = words[w];
cwl = cWord.length;
if ((ol + cwl) <= l) {
output.push(cWord);
ol += cwl + 1;
} else {
break;
}
}
return output.join(" ") + p;
};
//USAGE
myText = "Bah bah, black sheep, have you any wool?";
cutRhyme = myText.reduce(10, "...");
trace("the cut string is : " + cutRhyme);
//outputs
//Bah bah black sheep...
from FlashGuru
