Javascript slicing with 3 parameters
I am attempting to slice my text, in such a way that if the character lenght of a text exceeds the given value, the text should slice from 0 to the value + also go until the next dot is found, so that we avoid slicing a text midsentence.
I've attempted the following
function shorten(text, num)
{
var fulltext=text.toString();
var firstdot = fulltext.indexOf(".");
if (fulltext.length < num)
{
return fulltext;
}
else
{
return fulltext.slice(0, (num + fulltext.indexOf(".")));
}
}
So the logic is, if the text is below the given value, full text is returned, if not i want to slice the text, but also include the text, until next dot. So once the num value has been reached in characters, it should keep searching until it finds the next dot, and then stop.
Example of what i want given lets say 10 as the num value. "Hello. This is a test example. This part should be removed."
Should return: "Hello. This is a test example." but instead it will return "Hello. This"
from Recent Questions - Stack Overflow https://ift.tt/3CWDAXv
https://ift.tt/eA8V8J
Comments
Post a Comment