Ruby on Rails:如何从字符串中删除/剪切前300个单词或字符?

shi*_*bly 2 ruby ruby-on-rails

我需要从字符串中删除/剪切前300个单词或字符.

这意味着,从一开始我就需要字符串中有限数量的字符.

截断的东西.

有这个功能吗?

Gui*_*nal 15

str = "many words here words words words ..."
first_500_words = str.split(" ").first(500).join(" ")
first_500_chars = str[0..500]
Run Code Online (Sandbox Code Playgroud)

  • `#first(500)`会更优雅 (8认同)