在rails中仅显示字符串的前x个单词 [英] Displaying Only the first x words of a string in rails
本文介绍了在rails中仅显示字符串的前x个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<%= message.content %>
我可以显示这样的消息,但在某些情况下我只想显示字符串的前 5 个单词,然后显示省略号 (...)
I can display a message like this, but in some situations I would like to display only the first 5 words of the string and then show an ellipsis (...)
推荐答案
在 rails 4.2
中你可以使用 truncate_words.
In rails 4.2
you can use truncate_words.
'Once upon a time in a world far far away'.truncate_words(4)
=> "Once upon a time..."
这篇关于在rails中仅显示字符串的前x个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文