我有一些密码:

line_width = 26

ch3 = "Chapter 3: Letters page 13"
puts(ch3.ljust(line_width/2) + ch3.rjust(line_width/2))

#=> Chapter 3: Letters page 13Chapter 3: Letters page 13

我希望将输出分成中间部分,如下所示:
Half-Text__________________________________________________Half-Text

我该怎么做?

最佳答案

你得先把课文分成两半。

line_width = 26

ch3 = "Chapter 3: Letters page 13"

ch3 = ch3.split(/\s(?=page)/) #=> ["Chapter 3: Letters", "page 13"]

puts(ch3[0].ljust(line_width / 2) + ch3[1].rjust(line_width / 2))

这个输出
Chapter 3: Letters      page 13

07-27 20:22