gpt4 book ai didi

javascript - 仅在评论中显示链接的一小部分

转载 作者:行者123 更新时间:2023-11-28 20:01:21 24 4
gpt4 key购买 nike

如果用户在评论中插入了链接,则该链接需要被 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com