Get a substr of a string using length of words instead of characters (SOLVED)

At the moment I’d like to split a string into two without cutting a word in half.

So far, I can get the word count of the string:

{{ $someString | countwords }}

However, I haven’t figured out a way to get half of the string without cutting a word in half.

For example, I am able to get the character length of $someString, then divide by 2, and then using substr extract half of the string, which works, but it cuts words in half if the midpoint happens to be in the middle of a word.

How can I split the string midway at the whitespace instead of cutting a word in half?

Thanks in advance for any help!


I decided to split $someString like so: {{ $subStr := split $string " " }} into substrings.

Then, I get the length of $subStr by doing the following: {{ $subStrLen:= len $subStr }}.

Next, I get the value for half of the length of the array of substrings: {{ $subStrHalfVal := sub $subStrLen (div $subStrLen 2) }}

I then save the first half of the substrings: {{ $subStrHalf := first $subStrHalfVal $subStr }}.

Now that I have half of the substrings that I want, how do I convert them back to one string? So far I’ve tried {{ string $subStrHalf }} to no avail.

Again, any help is much appreciated.


I used delimit to convert the substrings back to a string: {{ delimit ($subStrHalf) " " }}

Problem solved =)