Rails -- 中文字符串截取的函数

来源:互联网 发布:大学生分期软件 编辑:程序博客网 时间:2024/05/17 01:50

字符串截取

truncate("Once upon a time in a world far far away")# => "Once upon a time in a world..." 

length是需要截取的字符串长度,包括中文和英文字符,如果不写,则有默认值,是28

truncate("And they found that many people were sleeping better.", :length => 25, :omission => '... (省略)')  # => "And they f... (省略)"  

omission是截取后末尾添加的字符结尾,默认是三个点的省略号。

如果字符串未达到指定长度,则不会发生变化。

简单示范一:

truncate("Once upon a time in a world far far away", :length=> 14)=> Once upon a...

因为omission没有指定取值,所以默认为”…”,所以在字符串末尾加上”…”

单复数

pluralize(1, 'person')  # => 1 person  pluralize(2, 'person')  # => 2 people  pluralize(3, 'person', 'users')  # => 3 users  pluralize(0, 'person')  # => 0 people 

一般用于 view 层

原文链接:http://blog.csdn.net/dazhi_100/article/details/9632713

0 0
原创粉丝点击