显示一定长度的字符串而不会被截断

时间:2019-08-10 07:34:55

标签: html ruby string slice

我正在显示一定长度的红宝石字符串。该字符串中只能显示80个字符。例如,如果字符串的长度为82,那么如果长度为250,它将显示为2行;如果字符串的长度为250,则字符串将显示为5行,以此类推,我想在空格上拆分而不是单词。

我是新来的,所以不知道该怎么解决。

4 个答案:

答案 0 :(得分:4)

def wrap(str, max_line_len)
  str.scan /(?<=\A| ).{1,#{max_line_len}}(?= |\z)/
end

str = "Little Miss Muffet she sat on her tuffet, eating her curds and whey. Along " + 
      "came a spider who sat down beside her and frightened Miss Muffet away."

         1         2         3    
123456789012345678901234567890123

puts wrap(str, 31)
Little Miss Muffet she sat on
her tuffet, eating her curds
and whey. Along came a spider
who sat down beside her and
frightened Miss Muffet away.

puts wrap(str, 32)
Little Miss Muffet she sat on
her tuffet, eating her curds and
whey. Along came a spider who
sat down beside her and
frightened Miss Muffet away.

puts wrap(str, 33)
Little Miss Muffet she sat on her
tuffet, eating her curds and
whey. Along came a spider who sat
down beside her and frightened
Miss Muffet away.

请参见String#scan。正则表达式为“ 1max_line_len字符之间的匹配,紧跟在字符串开头或空格之后,紧跟在空格或字符串结尾之后”。 (?<=\A| )正向后看,而{{11}}是正向后看

答案 1 :(得分:1)

只是出于好奇:

loop.inject([[], input]) do |(acc, src)|
  if m = src[/.{1,79}(\s|\z)/]
    [acc << m, $']
  else
    break acc << src
  end
end

答案 2 :(得分:0)

一个非常快速,肮脏的迭代版本:

max_chars = 80
text = 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.'

lines = []
words = text.split

while words.length > 0
  line = words.shift

  while words.first && (line.length + words.first.length + 1) <= max_chars
    line << " #{words.shift}" 
  end

  lines << line
end

lines.each { |line|  puts line }
#=> Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod
#   tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At
#   vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren,
#   no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit
#   amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut
#   labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam
#   et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata
#   sanctus est Lorem ipsum dolor sit amet.

答案 3 :(得分:0)

其他选项,不是很干净,但是...

想法是找到空格的索引,并用\n替换索引中接近行长的空格。

因此,给定字符串strmax_len

delta = 0
(str + " ")
.each_char.with_index.with_object([]) { |(c, i), o| o << i if c == " "} # find the index of the spaces
.each_cons(2).with_object([]) do |(a, b), tmp| # select the index to be substituted
  if b > (tmp.size + 1) * max_len + delta
    tmp << a 
    delta = tmp.last - max_len * tmp.size + 1
  end
end.each { |i| str[i] = "\n" } # substitute

现在,当行长接近str时,\n具有max_len。 这将更改原始字符串。