truncate string hs code example
Example 1: Truncate a String
const truncateString = (str, num) => {
return num >= str.length ? str : str.slice(0, num)+'...'
let newString = ''
if(num >= str.length) newString += str;
else newString += str.split('').splice(0, num).join('')+'...'
return newString
let truncateString = ''
if(num >= str.length) {
truncateString = str
return truncateString
}
else for(let i = 0; i < num; i++) truncateString += str[i]
return truncateString+'...'
let truncateString = ''
for(let i = 0; i < num; i++) truncateString += str[i]
return num >= str.length ? str : truncateString+'...'
}
truncateString("Peter Piper picked a peck of pickled peppers", 11);
Example 2: Truncate a string
function truncateString(str, num) {
if (str.length > num) {
return str.slice(0, num) + "...";
} else {
return str;
}
}
truncateString("Lorem Ipsum placeholder text in any number of characters, words sentences or paragraphs", 9)