我目前正在尝试截断任何超过65个字符的字符串.
我的代码是
它适用于超过65个字符的标题.但是正好65个字符的标题仍然会被截断.
例如:
title:”This post is exactly 65 characters characters characters
characte”shows on page as “This post is exactly 65 characters characters
characters chara…”
我不应该使用truncate吗?
最佳答案
truncate是正确的方法.这可能是您的rails版本中的错误?这是我在控制台上得到的内容:
原文链接:https://www.f2er.com/html/426862.html[5] pry(main)> helper.truncate("This post is exactly 56 characters characters characters characte",length: 65)
=> "This post is exactly 56 characters characters characters characte"
[6] pry(main)> helper.truncate("This post is exactly 56 characters characters characters characte",length: 64)
=> "This post is exactly 56 characters characters characters char..."
我在这个例子中运行Rails 4.0.4.