I have a bunch of title texts that get generated they all have different .Length but at a specific startindex of the string I want to find the closest space and then remove the text after it and also the space, and then add "...".
The most important part is that it shouldnt extend 49 length
Example:
"What can UK learn from Spanish high speed rail when its crap"
I want to make sure that it becomes:
"What can UK learn from Spanish high speed rail..."
How can I do this with Jquery?
I have a C# code that acheive this:
public static string TrimLength(string text, int maxLength)
{
if (text.Length > maxLength)
{
maxLength -= "...".Length;
maxLength = text.Length < maxLength ? text.Length : maxLength;
bool isLastSpace = text[maxLength] == ' ';
string part = text.Substring(0, maxLength);
if (isLastSpace)
return part + "...";
int lastSpaceIndexBeforeMax = part.LastIndexOf(' ');
if (lastSpaceIndexBeforeMax == -1)
return part + "...";
else
return text.Substring(0, lastSpaceIndexBeforeMax) + "...";
}
else
return text;
}
But I have no idea how to do this with jquery
Any kind of help is appreciated or any kind of tips on how to achieve this.