我想知道是否有一种简单的方法可以从字符串中仅获取N个符号,而不会删除整个单词。
例如,我有产品和产品描述信息。描述长度为70到500个符号,但我想只显示前70个符号,如下所示:
可口可乐是最受欢迎和最畅销的软饮料 历史,以及世界上最知名的品牌。
2011年5月8日,可口可乐庆祝其125周年纪念日。创建于 1886年在佐治亚州亚特兰大市,由John S. Pemberton博士,可口可乐公司 首先通过混合在Jacob's Pharmacy作为喷泉饮料提供 可口可乐糖浆与碳酸水。
所以,普通的子字符串方法会给我:
Coca-Cola is the most popular and biggest-selling soft drink in histor
我需要一种方法来获得这个:
Coca-Cola is the most popular and biggest-selling soft drink in ...
答案 0 :(得分:7)
只需使用truncate with separator选项:
truncate("Once upon a time in a world far far away", length: 17)
# => "Once upon a ti..."
truncate("Once upon a time in a world far far away", length: 17, separator: ' ')
# => "Once upon a..."
答案 1 :(得分:5)
此方法使用正则表达式,贪婪地抓取多达70个字符,然后匹配字符串的空格或结尾以实现您的目标
def truncate(s, max=70, elided = ' ...')
s.match( /(.{1,#{max}})(?:\s|\z)/ )[1].tap do |res|
res << elided unless res.length == s.length
end
end
s = "Coca-Cola is the most popular and biggest-selling soft drink in history, as well as the best-known brand in the world."
truncate(s)
=> "Coca-Cola is the most popular and biggest-selling soft drink in ..."
答案 2 :(得分:3)
s = "Coca-Cola is the most popular and biggest-selling soft drink in history, as well as the best-known brand in the world."
s = s.split(" ").each_with_object("") {|x,ob| break ob unless (ob.length + " ".length + x.length <= 70);ob << (" " + x)}.strip
#=> "Coca-Cola is the most popular and biggest-selling soft drink in"
答案 3 :(得分:1)
s[0..65].rpartition(" ").first << " ..."
在你的考试中:
s = "Coca-Cola is the most popular and biggest-selling soft drink in history, as well as the best-known brand in the world."
t = s[0..65].rpartition(" ").first << " ..."
=> "Coca-Cola is the most popular and biggest-selling soft drink in ..."
答案 4 :(得分:0)
(受dbenhur的回答启发,但更好地处理第一个最大字符中没有空格或字符串结尾的情况。)
def truncate(s, options = { })
options.reverse_merge!({
max: 70,
elided: ' ...'
});
s =~ /\A(.{1,#{options[:max]}})(?:\s|\z)/
if $1.nil? then s[0, options[:max]] + options[:elided]
elsif $1.length != s.length then $1 + options[:elided]
else $1
end
end
答案 5 :(得分:0)
library(data.table)
uber_names <- dt$uber_name
dt[, uber_write_filtered := gsub(
pattern = paste0("(", paste(uber_names, collapse = "|"), ")"),
replacement = "", uber_write)]