shibly shibly - 1 month ago 5
Ruby Question

Ruby on Rails: How can i take/cut first 300 words or characters from a string?

I need to take/cut first 300 words or characters from a string.

That means, I need a limited number of characters from a string, from the beginning.

Something like truncating.

Is there a function to do this?

Answer
str = "many words here words words words ..."
first_500_words = str.split(" ").first(500).join(" ")
first_500_chars = str[0..500]
Comments