Shorten a string with complete words

Shorten a string with complete words
The Excerpt() function takes in a string and shortens it to the desired amount of characters, but without cutting into the middle of a word.
1.def Excerpt (FullString, CharCount = 200)
2.        if FullString.length >= CharCount
3.                ExcFullString = FullString[0, CharCount]
4.                SplitFullString = ExcFullString.split(/\s/)
5.                FullWords = SplitFullString.length
6.                SplitFullString[0, FullWords-1].join(" ") + '...'
7.        else
8.                FullString
9.        end
10.end

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top