title: How can I trim a text like this with jquery?
body: <p>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 "...".</p>
  
  <p>The most important part is that it shouldnt extend 49 length</p>
  
  <p>Example:</p>
  
  <pre><code>"What can UK learn from Spanish high speed rail when its crap"
  </code></pre>
  
  <p>I want to make sure that it becomes:</p>
  
  <pre><code>"What can UK learn from Spanish high speed rail..."
  </code></pre>
  
  <p>How can I do this with Jquery?</p>
  
  <p>I have a C# code that acheive this:</p>
  
  <pre><code>public static string TrimLength(string text, int maxLength)
  {
      if (text.Length &gt; maxLength)
      {
          maxLength -= "...".Length;
          maxLength = text.Length &lt; 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;
  }
  </code></pre>
  
  <p>But I have no idea how to do this with jquery</p>
  
  <p>Any kind of help is appreciated or any kind of tips on how to achieve this.</p>