Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit string by number of words #68

Open
omundy opened this issue Dec 7, 2023 · 2 comments
Open

Limit string by number of words #68

omundy opened this issue Dec 7, 2023 · 2 comments

Comments

@omundy
Copy link

omundy commented Dec 7, 2023

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(" ") + "...

@marcuspoehls
Copy link
Member

@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 👍

@omundy
Copy link
Author

omundy commented Dec 8, 2023

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()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants