0

假设我在一个名为“tao”的文件中有这个:

If you can talk about it, it ain't Tao. If it has a name, it's just another thing.  
Tao doesn't have a name. Names are for ordinary things.

我想用红宝石打开它,并将其更改为:

If you can talk about it, it ain't Tao. If it has a name, it's just another thing.
  
Tao doesn't have a name. Names are for ordinary things.

我知道如何读取文件,并将内容放入字符串(我们称之为tao_string),但我不确定如何将单换行符更改为双换行符。我会通过正则表达式怀疑某些东西,但我不知道从哪里开始。

4

1 回答 1

0

你可以这样做gsub

tao_string2 = tao_string.gsub("\n", "\n\n")

或就地gsub!

tao_string.gsub!("\n", "\n\n")

这将用双换行符替换字符串中的所有换行符。

于 2012-05-31T11:09:29.227 回答