将输出格式化为每行40个字符 [英] Format output to 40 characters long per line

查看:174
本文介绍了将输出格式化为每行40个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Ruby相当陌生,现在我一直在Google上搜索几个小时。
有人知道如何格式化打印的输出不超过40个字符长吗?例如:



我要打印的内容:

 这是一个简单的句子。 
这个简单的
句子在四行上显示

但是我想把它格式化为:

 这是一个简单的句子。这个简单的
句子出现在四行上。

我把原来的每一行放入一个数组中。

so x = [这是一个简单的句子,这个简单的,出现的句子,在三行上。]

我试过 x.each {| n |打印n [0..40],} 但它似乎没有做任何事情。


任何帮助都很棒!

方法 word_wrap 需要一个Strind,并打印出一个漂亮的样子。



你的数组被转换成一个字符串, code> join(\\\



代码:

  def word_wrap(text,line_width = 40)
如果line_width <= 0则返回文本
text.gsub(/ \\\ n /,'') .gsub(/(。{1,#{line_width}})(\s + | $)/,\\1\\\
)strip
end

x =这是一个简单的句子,这个简单的,出现的句子,在三行上。]

把word_wrap(x.join(\\\
))
x<< 'a'* 50#为了显示长词的含义
x<< 'end'
puts word_wrap(x.join(\ n))

代码解释:

x.join(\\\
))
建立一个字符串,然后建立一个long与 text.gsub(/ \ n /,'')>一致。
在这个特殊情况下,这两个步骤可以合并: x.join())



})(\s + | $)/,\\1\\\




  • (。{1,#{line_width}})):取任何字符< line_width
  • (\s + | $):下一个字符必须是空格或行结束符单词:如果最后一个字符没有空格,那么以前的匹配可能会短于 line_width

  • \\ \\\1\\\
    :取最多40个字符的字符串,并用换行符结束。 / code>重复包装,直到完成。



最后,我删除前导和尾随空格 strip



我还添加了一个长字(50个字符),会发生什么?gsub不匹配,保持原样。


I'm fairly new to Ruby and I've been searching Google for a few hours now. Does anyone know how to format the output of a print to be no more than 40 characters long?

For example:

What I want to print:

This is a simple sentence.
This simple
sentence appears
on four lines. 

But I want it formatted as:

This is a simple sentence. This simple
sentence appears on four lines.

I have each line of the original put into an array.
so x = ["This is a simple sentence.", "This simple", "sentence appears", "on three lines."]
I tried x.each { |n| print n[0..40], " " } but it didn't seem to do anything.

Any help would be fantastic!

解决方案

The method word_wrap expects a Strind and makes a kind of pretty print.

Your array is converted to a string with join("\n")

The code:

def word_wrap(text, line_width = 40 ) 
  return text if line_width <= 0
  text.gsub(/\n/, ' ').gsub(/(.{1,#{line_width}})(\s+|$)/, "\\1\n").strip
end

x = ["This is a simple sentence.", "This simple", "sentence appears", "on three lines."]

puts word_wrap(x.join("\n"))
x << 'a' * 50 #To show what happens with long words
x << 'end'
puts word_wrap(x.join("\n"))

Code explanation:

x.join("\n")) build a string, then build one long line with text.gsub(/\n/, ' '). In this special case this two steps could be merged: x.join(" "))

And now the magic happens with

gsub(/(.{1,#{line_width}})(\s+|$)/, "\\1\n")

  • (.{1,#{line_width}})): Take any character up to line_width characters.
  • (\s+|$): The next character must be a space or line end (in other words: the previous match may be shorter the line_width if the last character is no space.
  • "\\1\n": Take the up to 40 character long string and finish it with a newline.
  • gsub repeat the wrapping until it is finished.

And in the end, I delete leading and trailing spaces with strip

I added also a long word (50 a's). What happens? The gsub does not match, the word keeps as it is.

这篇关于将输出格式化为每行40个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆