如何从Ruby字符串中删除“\n”字符串?

时间:2021-05-03 00:10:41

I have this string:

我有这个字符串:

"some text\nandsomemore"

I need to remove the "\n" from it. I've tried

我需要从它中删除“\n”。我试过了

"some text\nandsomemore".gsub('\n','')

but it doesn't work. How do I do it? Thanks for reading.

但它不工作。我该怎么做呢?感谢你的阅读。

6 个解决方案

#1


137  

You need to use "\n" not '\n' in your gsub. The different quote marks behave differently.

你需要使用“\n”而不是“\n”在你的gsub。不同的引号有不同的表现。

Double quotes " allow character expansion and expression interpolation ie. they let you use escaped control chars like \n to represent their true value, in this case, newline, and allow the use of #{expression} so you can weave variables and, well, pretty much any ruby expression you like into the text.

双引号允许字符扩展和表达式插补。它们允许您使用诸如\n之类的转义控件符号来表示它们的真实值(在本例中是newline),并允许使用#{expression},以便您可以将变量和几乎所有您喜欢的ruby表达式编织到文本中。

While on the other hand, single quotes ' treat the string literally, so there's no expansion, replacement, interpolation or what have you.

而另一方面,单引号将字符串按字面意思处理,所以没有扩展,替换,插值,或者你有什么。

In this particular case, it's better to use either the .delete or .tr String method to delete the newlines.

在这种情况下,最好使用.delete或.tr字符串方法来删除新行。

See here for more info

更多信息请看这里。

#2


52  

When you want to remove a string, rather than replace it you can use String#delete (or its mutator equivalent String#delete!), e.g.:

当你想删除一个字符串,而不是替换它时,你可以使用string #delete(或者它的mutator等价的string #delete),例如:

x = "foo\nfoo"
x.delete!("\n")

x now equals "foofoo"

现在x =“foofoo”

In this specific case String#delete is more readable than gsub since you are not actually replacing the string with anything.

在这种特定的情况下,字符串#delete比gsub更具可读性,因为您实际上并没有用任何东西替换字符串。

#3


35  

If you want or don't mind having all the leading and trailing whitespace from your string removed you can use the strip method.

如果您想要或不介意删除字符串中的所有前导和后拖空格,可以使用strip方法。

"    hello    ".strip   #=> "hello"   
"\tgoodbye\r\n".strip   #=> "goodbye"

as mentioned here.

如前所述。

edit The original title for this question was different. My answer is for the original question.

编辑这个问题的原始标题是不同的。我的回答是针对最初的问题。

#4


21  

You don't need a regex for this. Use tr:

你不需要正则表达式。使用tr:

"some text\nandsomemore".tr("\n","")

#5


11  

use chomp or strip functions from Ruby:

使用Ruby中的chomp或strip函数:

"abcd\n".chomp => "abcd"
"abcd\n".strip => "abcd"

#6


3  

Look here:

看这里:

#1


137  

You need to use "\n" not '\n' in your gsub. The different quote marks behave differently.

你需要使用“\n”而不是“\n”在你的gsub。不同的引号有不同的表现。

Double quotes " allow character expansion and expression interpolation ie. they let you use escaped control chars like \n to represent their true value, in this case, newline, and allow the use of #{expression} so you can weave variables and, well, pretty much any ruby expression you like into the text.

双引号允许字符扩展和表达式插补。它们允许您使用诸如\n之类的转义控件符号来表示它们的真实值(在本例中是newline),并允许使用#{expression},以便您可以将变量和几乎所有您喜欢的ruby表达式编织到文本中。

While on the other hand, single quotes ' treat the string literally, so there's no expansion, replacement, interpolation or what have you.

而另一方面,单引号将字符串按字面意思处理,所以没有扩展,替换,插值,或者你有什么。

In this particular case, it's better to use either the .delete or .tr String method to delete the newlines.

在这种情况下,最好使用.delete或.tr字符串方法来删除新行。

See here for more info

更多信息请看这里。

#2


52  

When you want to remove a string, rather than replace it you can use String#delete (or its mutator equivalent String#delete!), e.g.:

当你想删除一个字符串,而不是替换它时,你可以使用string #delete(或者它的mutator等价的string #delete),例如:

x = "foo\nfoo"
x.delete!("\n")

x now equals "foofoo"

现在x =“foofoo”

In this specific case String#delete is more readable than gsub since you are not actually replacing the string with anything.

在这种特定的情况下,字符串#delete比gsub更具可读性,因为您实际上并没有用任何东西替换字符串。

#3


35  

If you want or don't mind having all the leading and trailing whitespace from your string removed you can use the strip method.

如果您想要或不介意删除字符串中的所有前导和后拖空格,可以使用strip方法。

"    hello    ".strip   #=> "hello"   
"\tgoodbye\r\n".strip   #=> "goodbye"

as mentioned here.

如前所述。

edit The original title for this question was different. My answer is for the original question.

编辑这个问题的原始标题是不同的。我的回答是针对最初的问题。

#4


21  

You don't need a regex for this. Use tr:

你不需要正则表达式。使用tr:

"some text\nandsomemore".tr("\n","")

#5


11  

use chomp or strip functions from Ruby:

使用Ruby中的chomp或strip函数:

"abcd\n".chomp => "abcd"
"abcd\n".strip => "abcd"

#6


3  

Look here:

看这里: