如果用户在评论中插入了链接,则该链接需要被 chop 为 20 个字符。
如何制作?
例如:
Lorem ipsum: https://github.com/plataformatec/devise
结果应该是这样。
Lorem ipsum: https://github.com/plataforma...
最佳答案
你想从哪一端 chop ?
使用String#[]
您可以从任意一端获取子字符串:
s = "string with more than twenty characters"
s[0, 20] # get the first 20 characters
=> "string with more tha"
s[s.size-20, s.size] # the last 20 characters
=> "an twenty characters"
Rails 还添加了 truncate字符串方法:
"string with more than twenty characters".truncate(20)
=> "string with more ..."
"string with more than twenty characters".truncate(20, omission: "")
=> "string with more tha"
希望有帮助。
关于javascript - 仅在评论中显示链接的一小部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21611591/