Reputation: 1734
I have a js string and I wanted to truncate it after 43 char and add three dots (...) at end. Three dots can be em dash.
Now the problem is if there was a whitespace at the end with truncated string I wanted to disallow it. Like, the funcation will skipp whitespace at the end while char count and add three dots.
getTruncatedName = (source) => {
let skippedString = source.trimEnd();
if(skippedString.length > 43){
return skippedString.substring(0, 43) + '...';
}else{
return source;
}
}
Diamond and Mother-of-pearl Circle Pendant in gold plate
Output:
Diamond and Mother-of-pearl Circle Pendant ...
I want :
Diamond and Mother-of-pearl Circle Pendant...
If i count before ... its 42 but i want 43
Upvotes: 1
Views: 3156
Reputation: 31
Does it always put a space there? Or could it be that there is a space at the 43th position and thus that is part of the string and included.
So maybe try re-trimming again. Like such
return skippedString.substring(0, 43).trimEnd() + '...';
Upvotes: 2