作者热门文章
- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我想将一个字符串拆分成 block ,每个 block 都在最大字符数内,比如 2000,并且不拆分单词。
我尝试过如下操作:
text.chars.each_slice(2000).map(&:join)
但有时,单词会被拆分。我尝试了一些正则表达式:
text.scan(/.{1,2000}\b|.{1,2000}/).map(&:strip)
来自 this question , 但我不太明白它是如何工作的,它给了我一些不稳定的行为,有时会给出只包含句点的 block 。
任何指点将不胜感激。
最佳答案
代码
def max_groups(str, n)
arr = []
pos = 0
loop do
break arr if pos == str.size
m = str.match(/.{1,#{n}}(?=[ ]|\z)|.{,#{n-1}}[ ]/, pos)
return nil if m.nil?
arr << m[0]
pos += m[0].size
end
end
示例
str = "Now is the time for all good people to party"
# 12345678901234567890123456789012345678901234
# 0 1 2 3 4
max_groups(str, 5)
#=> nil
max_groups(str, 6)
#=> ["Now is", " the ", "time ", "for ", "all ", "good ", "people", " to
max_groups(str, 10)
#=> ["Now is the", " time for ", "all good ", "people to ", "party"]
max_groups(str, 14)
#=> ["Now is the ", "time for all ", "good people to", " party"]
max_groups(str, 15)
#=> ["Now is the time", " for all good ", "people to party"]
max_groups(str, 29)
#=> ["Now is the time for all good ", "people to party"]
max_groups(str, 43)
#=> ["Now is the time for all good people to ", "party"]
max_groups(str, 44)
#=> ["Now is the time for all good people to party"]
str = "How you do?"
# 123456789012345678
# 0 1
max_groups(str, 4)
#=> ["How ", " ", " ", "you ", "do?"]
关于ruby - 将字符串拆分为最大字符数的 block 而不打断单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49087131/
我是一名优秀的程序员,十分优秀!