I have a string as shown below which I want to truncate it after a particular character limit and display an ellipsis.
"This is a long string"
On truncation, I want the string to display something like this:
This is a long (...)
or
This is a (...)
or
This is (...)
The above examples don't cut the words. What I don't want is:
This is a lon (...)
or
This is a long s (...)
The above examples don't work.
I am using the following function in react to truncate a string. The problem with the function is that it sometimes cut the words. The value of length I am using is 175. For 175 character limit, sometimes it cut the words and sometimes it doesn't.
export const wordsTruncate = (words, length) => {
const j = words.split('');
let result = j.filter((z, a) => a <= (length - 1));
return result.join("");
}
I am wondering what changes I need to make in the function above so that it doesn't cut the words as shown in the examples above.