从 Ruby 中的字符串修剪空白换行符 [英] Trim blank newlines from string in Ruby
本文介绍了从 Ruby 中的字符串修剪空白换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一串四行空行,总共八行,如下所示:
I have a string of four blank lines which all up makes eight lines in total in the following:
str = "aaa\n\n\nbbb\n\nccc\ddd\n"
我想在一行中全部归还.输出应该是这样的:
I want to return this all in one line. The output should be like this on a single line:
aaabbbcccddd
我使用了各种修剪函数来获取输出,但仍然失败.
I used various trim functions to get the output but still I am failing.
这里我必须使用什么方法?
What method do I have to use here?
推荐答案
Ruby(稍微少一点 Perl 风格)方式:
The Ruby (and slightly less Perl-ish) way:
new_str = str.delete "\n"
...或者如果您想就地进行:
...or if you want to do it in-place:
str.delete! "\n"
这篇关于从 Ruby 中的字符串修剪空白换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文