strings
strings copied to clipboard
Limit string by number of words
Is there a way to limit a string by the number of words? I tried a few ways to get at it.
Str("To be or not to be").words().limit(3).get()
I'm doing it right now with:
Str(text).words().slice(0,5).join(" ") + "...
@omundy Hey Owen, the words method returns a JavaScript array of strings. You're doing it right by slicing the number of words from the array 👍
I could also just do this without the library:
text.split(" ").slice(0,5).join(" ") + "..."
Perhaps something like this would be an acceptable addition?
text.truncate(5, "...").get()