title: Truncate string in Liquid and remove last word
body: <p>I'm using Liquid within a Jekyll-powered blog and currently trying to truncate various strings to a fixed size.</p>
  <p>However, both built-in filters don't produce the output I'm trying to procude.</p>
  <p>If I use <a href="https://shopify.github.io/liquid/filters/truncate/" rel="nofollow noreferrer"><code>truncate</code></a>, I'm able to cut the string after a given number of characters:</p>
  <pre><code>{{ &quot;Ground control to Major Tom.&quot; | truncate: 24 }} -&gt; Ground control to Maj…
  </code></pre>
  <p>On the other hand, <a href="https://shopify.github.io/liquid/filters/truncatewords/" rel="nofollow noreferrer"><code>truncatewords</code></a> allows me to shorten a string after a given number of words.</p>
  <pre><code>{{ &quot;Ground control to Major Tom.&quot; | truncatewords: 4 }} -&gt; Ground control to Major…
  </code></pre>
  <p><strong>However, I want to truncate a string after <code>x</code> characters and then remove the last word in order to not have fragmented words left at the end of the string.</strong></p>
  <p>In the above example, my desired output after truncating 24 characters would be: <code>Ground control to…</code></p>
  <p>I tried to play around with the filter <code>last</code>, but didn't manage to find a suitable solution.</p>
  <p>Any help is very appreciated!</p>