Ruby 截断字符串的部分以限制整个字符串';红宝石中的s长度
假设要生成如下所示的动态页面标题: “这都是一个梦想,我以前读过臭名昭著的B.I.G.的《多汁》中的《WordUp杂志》 例如,艺术家“SONG_NAME”中的“歌词” 但是,您的标题总共只能包含69个字符,此模板有时会生成更长的标题 解决此问题的一种策略是将整个字符串截断为69个字符。但是,更好的方法是先截断字符串中不太重要的部分。即,您的算法可能如下所示:Ruby 截断字符串的部分以限制整个字符串';红宝石中的s长度,ruby,string,Ruby,String,假设要生成如下所示的动态页面标题: “这都是一个梦想,我以前读过臭名昭著的B.I.G.的《多汁》中的《WordUp杂志》 例如,艺术家“SONG_NAME”中的“歌词” 但是,您的标题总共只能包含69个字符,此模板有时会生成更长的标题 解决此问题的一种策略是将整个字符串截断为69个字符。但是,更好的方法是先截断字符串中不太重要的部分。即,您的算法可能如下所示: 截断歌词,直到整个字符串都是我有一个类似的问题,就是截断标题,把它们放到tweet中。最后我把所有的东西都放在一个数组中并加入它。代码是
message = [lyrics, " from ", song_name, " by ", artist]
[0, 2, 4].each do |index|
length = message.join.length
message[index] = message[index][0...(69-length)]
end
message = message.join
您可能还希望在截断时附加省略号,以便人们知道它已被截断。这是一个相当完整的实现
#!/usr/bin/ruby
MIN_WORD_SIZE = 10
MAX_STRING_SIZE = 55 # 69 - 14 characters for punctuation spacing and conjunctions
def smart_trunc_words(lyrics, artist, title)
words = [lyrics, artist, title]
words.each_index do |idx|
total_length = words.to_s.length
if ( words[idx].length > MIN_WORD_SIZE and total_length > MAX_STRING_SIZE )
other_words = total_length - words[idx].length
max_word_size = [MAX_STRING_SIZE - other_words, MIN_WORD_SIZE].max
words[idx] = truncate_word words[idx], max_word_size
end
end
words
end
def truncate_word(word, sub_size)
# Enhance by adding ellipses here or break on a word boundary
word[0, sub_size]
end
def smart_trunc(lyrics, artist, title)
lyrics, artist, title = smart_trunc_words lyrics, artist, title
'"%s" from "%s" by %s' % [lyrics, title, artist]
end
test_data = [
[ "It was all a dream, I used to read word up magazine", "The Notorious B.I.G", "Juicy"],
[ "Ground Control to Major Tom, your circuits dead there's something wrong", "David Bowie", "Space Oddity"],
[ "Back Home they'll be thinking about us", "The England world cup squad (1970)" , "Back Home"],
[ "I'm the new cancer", "Panic at the disco", "Theres A Good Reason These Tables Are Numbered Honey, You Just Havent Thought Of It Yet"],
[ "Short lyrics", "short band", "short song"],
[ "VeryLongLyrics VeryLongLyrics VeryLongLyrics VeryLongLyrics VeryLongLyrics",
"VeryLongBandName VeryLongBandName VeryLongBandName VeryLongBandName VeryLongBandName",
"VeryLongTitle VeryLongTitle VeryLongTitle VeryLongTitle VeryLongTitle"]
]
test_data.each do |td|
lyrics = td[0]
artist = td[1]
title = td[2]
puts 'Original string : "%s" from "%s" by %s' % [lyrics, title, artist]
puts "New string : #{smart_trunc lyrics, artist, title}"
end
这将产生以下结果
Original string : "It was all a dream, I used to read word up magazine" from "Juicy" by The Notorious B.I.G
New string : "It was all a dream, I used to r" from "Juicy" by The Notorious B.I.G
Original string : "Ground Control to Major Tom, your circuits dead there's something wrong" from "Space Oddity" by David Bowie
New string : "Ground Control to Major Tom, you" from "Space Oddity" by David Bowie
Original string : "Back Home they'll be thinking about us" from "Back Home" by The England world cup squad (1970)
New string : "Back Home th" from "Back Home" by The England world cup squad (1970)
Original string : "I'm the new cancer" from "Theres A Good Reason These Tables Are Numbered Honey, You Just Havent Thought Of It Yet" by Panic at the disco
New string : "I'm the ne" from "Theres A Good Reason These Tables A" by Panic at t
Original string : "Short lyrics" from "short song" by short band
New string : "Short lyrics" from "short song" by short band
Original string : "VeryLongLyrics VeryLongLyrics VeryLongLyrics VeryLongLyrics VeryLongLyrics" from "VeryLongTitle VeryLongTitle VeryLongTitle VeryLongTitle VeryLongTitle" by VeryLongBandName VeryLongBandName VeryLongBandName VeryLongBandName VeryLongBandName
New string : "VeryLongLy" from "VeryLongTitle VeryLongTitle VeryLon" by VeryLongBa
原创字符串:臭名昭著的B.I.G.的《多汁》(Juicy)中的“这都是一个梦想,我过去常读《WordUp》杂志”
新串:臭名昭著的B.I.G.的《多汁》中的“这都是一个梦,我曾经是r”
原文:大卫·鲍伊的《太空奇遇》中的“地面控制呼叫汤姆少校,你的电路死了,出了点问题”
大卫·鲍伊的《太空奇遇》中的新弦乐:“地面指挥,汤姆少校,你”
英国世界杯足球队(1970年)的《回到家》中的原始字符串:“回到家,他们会想起我们的”
新串:英格兰世界杯足球队《老家》中的“老家th”(1970年)
原始字符串:“我是新的巨蟹座”来自“有一个很好的理由这些桌子都被编号了,亲爱的,你只是还没有想到它”在迪斯科的恐慌
新字符串:“我是ne”来自t的恐慌所写的“这些表格A有一个很好的理由”
原串:“短歌词”来自短小乐队的“短歌”
新弦乐:短歌乐队《短歌》中的“短歌词”
原始字符串:“verylonglyrpriss verylonglyspriss verylonglyspriss verylonglyspriss verylonglyspriss”来自VeryLongTitle VeryLongTitle VeryLongTitle VeryLongTitle VeryLongTitle VeryLongBandName VeryLongBandName VeryLongBandName VeryLongBandName
新字符串:VeryLongBa的“VeryLongTitle VeryLongTitle VeryLon”中的“VeryLongLy”
毫无疑问,一些ruby大师会让它变得更短和/或更地道