How to get a substring of text?

前端 未结 5 1790
无人及你
无人及你 2020-12-02 14:48

I have text with length ~700. How do I get only ~30 of its first characters?

5条回答
  •  Happy的楠姐
    2020-12-02 15:43

    Since you tagged it Rails, you can use truncate:

    http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-truncate

    Example:

     truncate(@text, :length => 17)
    

    Excerpt is nice to know too, it lets you display an excerpt of a text Like so:

     excerpt('This is an example', 'an', :radius => 5)
     # => ...s is an exam...
    

    http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-excerpt

提交回复
热议问题