How can I cut a string after X characters?
Simply
var trunc = "abcdef".substr(0, 3) + "\u2026";
var trucatedText = yourtext.substring(0, 3) + '...'; // substring(from, to);
The other answers are great, but they always add an ellipsis. The following will only add an ellipsis if the string is too long:
function truncateText(text, length) {
if (text.length <= length) {
return text;
}
return text.substr(0, length) + '\u2026'
}
let truncated;
truncated = truncateText('Hello, World!', 10);
console.log(truncated);
truncated = truncateText('Hello, World!', 50);
console.log(truncated);
Sth like this?
var text= "This is your text";
var stripHere = 7;
var shortText = text.substring(0, stripHere) + "...";
alert(shortText);
http://jsfiddle.net/ThKsw/