将字符串拆分为块(不同大小)而不会破坏单词

时间:2014-07-21 17:49:21

标签: ruby string

我正在尝试创建一个方法,给定一个字符串,返回三个字符串:title,description1,description2

这是我发现的一个相关问题:Split a string into chunks of specified size without breaking words - 但我的块大小不同。

标题最多需要25个字符。

Description1最多需要35个字符。

Description2最多需要35个字符。

问题是:

如何分割字符串以便最多创建三个实体(注意:如果字符串只适合第一个实体,那我就不需要返回三个实体),其中第一个实体最多包含25个字符,另外两个最多包含35个字符。使方法足够聪明,可以考虑单词(也可能是标点符号),这样它就不会返回剪切结果。

我做了以下事情:

def split_text_to_entities(big_string)
  title = big_string(0..24)
  description1 = big_string(25..59)
  description2 = big_string(60..94)
end

但这种方法的问题在于,如果输入是"从我们的商店购买我们的新品牌鞋。镇上最优惠的折扣和首次购买的40%折扣。",结果将是:

title = "Buy our new brand shoes f"
description1 = "rom our store. Best discounts in to"
description2 = "wn and 40% off for first purchase."

理想情况下,他们会:

title = "Buy our new brand shoes"
description1 = "from our store. Best discounts in"
description2 = "town and 40% off for first"

因此,请尝试按字符大小进行拆分,并考虑单词。

3 个答案:

答案 0 :(得分:1)

这不起作用?:

def get_chunks(str, n = 3)
  str.scan(/^.{1,25}\b|.{1,35}\b/).first(n).map(&:strip)
end

答案 1 :(得分:1)

为了涵盖所有基础,我会做以下事情。

<强>代码

def divide_text(str, max_chars)
  max_chars.map do |n|
    str.lstrip!
    s = str[/^.{,#{n}}(?=\b)/] || ''
    str = str[s.size..-1]
    s
  end
end

(?=\b)是一个(零宽度)正向前瞻,与单词中断匹配,即空白字符或字符串结尾。

<强>实施例

max_nbr_chars = [25,35,35]

str = "Buy our new brand shoes from our store. Best discounts in " +
      "town and 40% off for first purchase."    
divide_text(str, max_nbr_chars)
  #=> ["Buy our new brand shoes",
  #    "from our store. Best discounts in",
  #    "town and 40% off for first"]

str = "Buy our new brand shoes from our store."
divide_text(str, max_nbr_chars)
  #=> ["Buy our new brand shoes", "from our store.", ""]

str = "Buy our new"
divide_text(str, max_nbr_chars)
  #=> ["Buy our new", "", ""]

str = ""
divide_text(str, max_nbr_chars)
  #=> ["", "", ""]

str = "Buyournewbrandshoesfromourstore."
divide_text(str, max_nbr_chars)
  #=> ["", "Buyournewbrandshoesfromourstore.", ""]

str = "Buyournewbrandshoesfromourstoreandshoesfromourstore."
divide_text(str, max_nbr_chars)
  #=> ["", "", ""]

请注意,如果正则表达式中省略了^

str = "Buyournewbrandshoesfromourstore."
divide_text(str, max_nbr_chars)
  #=> ["ewbrandshoesfromourstore.", "rstore.", ""]

答案 2 :(得分:0)

s = "Buy our new brand shoes from our store. Best discounts in town and 40% off for first purchase."

s =~ /\b(.{,25})\W+(.{,35})\W+(.{,35})\b/
[$1, $2, $3] # =>
# [
#   "Buy our new brand shoes",
#   "from our store. Best discounts in",
#   "town and 40% off for first purchase"
# ]
相关问题