在某个位置添加折断线

时间:2013-12-03 10:33:40

标签: ruby string format

如何格式化str

str = "2013-12-03 10:19:06 +0100: Success: 34 => ["reset/htx", "auth/mx", "auth/htx", "product", "product/id", "product/id/review", "search/offer", "search/ac", "location", "location/zone", "shop", "shop/id", "shop/id/category", "shop/id/contact", "shop/id/terms", "shop/id/return", "shop/id/oid/fragment", "retailer", "retailer/id", "user/id/account", "user/id/review", "user/id/review/id", "user/id/product", "user/id/product/id", "user/id/tag", "category", "category/urlName", "about", "about/terms", "about/privacy", "about/order", "about/order/state", "about/tp", "about/tp/abbrev"]"

获得此输出:

2013-12-03 10:19:06 +0100: Success: 25 => ["reset/htx", "auth/mx", "auth/htx", "product", "product/id", "product/id/review", 
                                           "search/offer", "search/ac", "location", "location/zone", "shop", "shop/id", 
                                           "shop/id/category", "shop/id/contact", "shop/id/terms", "shop/id/return", 
                                           "shop/id/oid/fragment", "retailer", "retailer/id", "user/id/account", 
                                           "user/id/review", "user/id/review/id", "user/id/product", "user/id/product/id"]

起初我只是尝试使用

添加断行
x.scan(/.{100}/).join("\n")

在单词之间添加断行。我也尝试过使用

str.split("").each_slice

但它也没有用。

1 个答案:

答案 0 :(得分:1)

如果我理解你的问题,可能没有一个简单的解决方案。这是您可以使用的一些代码。

str = %q{2013-12-03 10:19:06 +0100: Success: 34 => ["reset/htx", "auth/mx", "auth/htx", "product", "product/id", "product/id/review", "search/offer", "search/ac", "location", "location/zone", "shop", "shop/id", "shop/id/category", "shop/id/contact", "shop/id/terms", "shop/id/return", "shop/id/oid/fragment", "retailer", "retailer/id", "user/id/account", "user/id/review", "user/id/review/id", "user/id/product", "user/id/product/id", "user/id/tag", "category", "category/urlName", "about", "about/terms", "about/privacy", "about/order", "about/order/state", "about/tp", "about/tp/abbrev"]}

max_length = 100
timestamp, list = str.split(' => [')
result = ''
line = timestamp += ' => ['
list.split(' ').each do |item|
  if line.length + item.length > max_length
    result += line + "\n"
    line = ' ' * timestamp.length
  end
  line += item
end
result += line

puts result

返回:

2013-12-03 10:19:06 +0100: Success: 34 => ["reset/htx","auth/mx","auth/htx","product","product/id",
                                           "product/id/review","search/offer","search/ac",
                                           "location","location/zone","shop","shop/id",
                                           "shop/id/category","shop/id/contact","shop/id/terms",
                                           "shop/id/return","shop/id/oid/fragment","retailer",
                                           "retailer/id","user/id/account","user/id/review",
                                           "user/id/review/id","user/id/product",
                                           "user/id/product/id","user/id/tag","category",
                                           "category/urlName","about","about/terms","about/privacy",
                                           "about/order","about/order/state","about/tp",
                                           "about/tp/abbrev"]

但请注意,如果max_length的任何单个列表项太长,您将遇到麻烦。在上面的例子中,缩进是43个字符和max_length 100.单个列表项长于57个字符虽然必须被破坏,并且会导致上面的代码产生溢出行。