I need to shorten a string down..
Lets say we have a string with the length 500.
I only want the first part of it – max 180 characters, ending with the last word before reaching the 180. I don’t want to cut the string short in the middle of a word.
How is this achived? it does not have to perform all that well.. it is something that happens a couple of times a day, not more.
A really easy way is by using this regex:
The only problem with that one is that it could contain trailing whitespace. To fix that, we can add a little negative look-behind:
That should do the trick.