var string = "ABCDEFG";
var truncString = string.substring(0, 3); //Only keep the first 3 characters
console.log(truncString); //output: "ABC"
function truncateString(str, num) {
if (str.length > num) {
return str.slice(0, num) + "...";
} else {
return str;
}
}
const truncate = (str, max, suffix) => str.length < max ? str : `${str.substr(0, str.substr(0, max - suffix.length).lastIndexOf(' '))}${suffix}`;
// Example
truncate('This is a long message', 20, '...');
function truncateString(str, num) {
return str.length > num ? str.slice(0, num) + "..." : str;
}
var length = 3;
var myString = "ABCDEFG";
var myTruncatedString = myString.substring(0,length);
// The value of myTruncatedString is "ABC"
_.truncate('hi-diddly-ho there, neighborino');
// => 'hi-diddly-ho there, neighbo...'
_.truncate('hi-diddly-ho there, neighborino', { 'length': 24, 'separator': ' '});
// => 'hi-diddly-ho there,...'
_.truncate('hi-diddly-ho there, neighborino', { 'length': 24, 'separator': /,? +/});
// => 'hi-diddly-ho there...'
_.truncate('hi-diddly-ho there, neighborino', { 'omission': ' [...]'});
// => 'hi-diddly-ho there, neig [...]'
var length = 3;
var myString = "ABCDEFG";
var myTruncatedString = myString.substring(0,length);
// The value of myTruncatedString is "ABC"