从 Ruby 中的字符串中删除所有空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30227685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Removing all whitespace from a string in Ruby
提问by Ayush Mishra
How can I remove all newlines and spaces from a string in Ruby?
如何从 Ruby 中的字符串中删除所有换行符和空格?
For example, if we have a string:
例如,如果我们有一个字符串:
"123\n12312313\n\n123 1231 1231 1"
It should become this:
它应该变成这样:
"12312312313123123112311"
That is, all whitespaces should be removed.
也就是说,应该删除所有空格。
回答by Fund Monica's Lawsuit
You can use something like:
您可以使用以下内容:
var_name.gsub!(/\s+/, '')
Or, if you want to return the changed string, instead of modifying the variable,
或者,如果您想返回更改后的字符串,而不是修改变量,
var_name.gsub(/\s+/, '')
This will also let you chain it with other methods (i.e. something_else = var_name.gsub(...).to_ito strip the whitespace then convert it to an integer). gsub!will edit it in place, so you'd have to write var_name.gsub!(...); something_else = var_name.to_i. Strictly speaking, as long as there is at least one change made,gsub!will return the new version (i.e. the same thing gsubwould return), but on the chance that you're getting a string with no whitespace, it'll return niland things will break. Because of that, I'd prefer gsubif you're chaining methods.
这也可以让您将它与其他方法链接起来(即something_else = var_name.gsub(...).to_i去除空格然后将其转换为整数)。gsub!将就地编辑它,所以你必须写var_name.gsub!(...); something_else = var_name.to_i. 严格来说,只要至少做了一个更改,gsub!就会返回新版本(即gsub返回相同的东西),但是如果您得到一个没有空格的字符串,它会返回nil并且事情会休息。正因为如此,我更喜欢gsub你链接方法。
gsubworks by replacing any matches of the first argument with the contents second argument. In this case, it matches any sequence of consecutive whitespace characters (or just a single one) with the regex /\s+/, then replaces those with an empty string. There's also a block form if you want to do some processing on the matched part, rather than just replacing directly; see String#gsubfor more information about that.
gsub通过用内容第二个参数替换第一个参数的任何匹配项来工作。在这种情况下,它将任何连续空白字符序列(或仅一个)与 regex 匹配/\s+/,然后用空字符串替换它们。如果你想对匹配的部分做一些处理,而不是直接替换,还有一个块形式;String#gsub有关更多信息,请参阅。
The Ruby docs for the class Regexpare a good starting point to learn more about regular expressions -- I've found that they're useful in a wide variety of situations where a couple of milliseconds here or there don't count and you don't need to match things that can be nested arbitrarily deeply.
该课程的 Ruby 文档Regexp是学习更多关于正则表达式的一个很好的起点——我发现它们在各种各样的情况下都很有用,在这里或那里几毫秒不重要而你不重要'不需要匹配可以任意深度嵌套的东西。
As Genesuggested in his comment, you could also use tr:
正如Gene在他的评论中建议的那样,您还可以使用tr:
var_name.tr(" \t\r\n", '')
It works in a similar way, but instead of replacing a regex, it replaces every instance of the nth character of the first argument in the string it's called on with the nth character of the second parameter, or if there isn't, with nothing. See String#trfor more information.
它的工作原理类似的方式,但是,而不是更换一个正则表达式,它取代的每个实例ñ字符串中的第一个参数的个字符,它被称为上与ñ第二个参数的个字符,或者如果没有,一无所有。有关String#tr更多信息,请参阅。
回答by Cary Swoveland
You could also use String#delete:
你也可以使用String#delete:
str = "123\n12312313\n\n123 1231 1231 1"
str.delete "\s\n"
#=> "12312312313123123112311"
You could use String#delete!to modify strin place, but note delete!returns nilif no change is made
你可以使用String#delete!str就地修改,但如果没有进行更改,请注意delete!返回nil
回答by spickermann
Alternatively you could scanthe string for digits /\d+/and jointhe result:
或者,您可以scan使用数字字符串/\d+/和join结果:
string = "123\n\n12312313\n\n123 1231 1231 1\n"
string.scan(/\d+/).join
#=> "12312312313123123112311"
Please note that this would also remove alphabetical characters, dashes, symbols, basically everything that is not a digit.
请注意,这也会删除字母字符、破折号、符号,基本上所有不是数字的东西。

