Ruby 被空格分割

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13537920/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 05:32:27  来源:igfitidea点击:

Ruby split by whitespace

rubysplitwhitespace

提问by JJ Beck

How can I write a Ruby function that splits the input by any kind of whitespace, and remove all the whitespace from the result? For example, if the input is

如何编写一个 Ruby 函数,将输入拆分为任何类型的空格,并从结果中删除所有空格?例如,如果输入是

 aa bbb
cc    dd ee

Then return an array ["aa", "bbb", "cc", "dd", "ee"].

然后返回一个数组["aa", "bbb", "cc", "dd", "ee"]

回答by Ajedi32

This is the default behavior of String#split:

这是 的默认行为String#split

input = <<-TEXT
 aa bbb
cc    dd ee
TEXT

input.split

Result:

结果:

["aa", "bbb", "cc", "dd", "ee"]

This works in all versions of Ruby that I tested, including 1.8.7, 1.9.3, 2.0.0, and 2.1.2.

这适用于我测试过的所有 Ruby 版本,包括 1.8.7、1.9.3、2.0.0 和 2.1.2。

回答by Candide

The following should work for the example you gave:

以下应该适用于您提供的示例:

str.gsub(/\s+/m, ' ').strip.split(" ")

it returns:

它返回:

["aa", "bbb", "cc", "dd", "ee"]

Meaning of code:

代码含义:

/\s+/mis the more complicated part. \smeans white space, so \s+means one ore more white space letters. In the /mpart, mis called a modifier, in this case it means, multiline, meaning visit many lines, not just one which is the default behavior. So, /\s+/mmeans, find sequences of one or more white spaces.

/\s+/m是更复杂的部分。\s表示空格,因此\s+表示一个或多个空格字母。在这/m部分中,m称为修饰符,在这种情况下,它的意思是multiline,意思是访问多行,而不仅仅是默认行为的一行。所以,/\s+/m意思是找到一个或多个空格的序列。

gsubmeans replace all.

gsub意味着全部替换。

stripis the equivalent of trimin other languages, and removes spaces from the front and end of the string.

strip相当于trim在其他语言中,并从字符串的前端和末尾删除空格。

As, I was writing the explanation, it could be the case where you do end up with and end-line character at the end or the beginning of the string.

因为,我正在写解释,可能会出现这样的情况,即在字符串的末尾或开头以和结束行字符结束。

To be safe

为了安全

The code could be written as:

代码可以写成:

str.gsub(/\s+/m, ' ').gsub(/^\s+|\s+$/m, '').split(" ")

So if you had:

所以如果你有:

str = "\n     aa bbb\n    cc    dd ee\n\n"

Then you'd get:

然后你会得到:

["aa", "bbb", "cc", "dd", "ee"]

Meaning of new code:

新代码的含义:

^\s+a sequence of white spaces at the beginning of the string

^\s+字符串开头的一系列空格

\s+$a sequence of white spaces at the end of the string

\s+$字符串末尾的空格序列

So gsub(/^\s+|\s+$/m, '')means remove any sequence of white space at the beginning of the string and at the end of the string.

所以gsub(/^\s+|\s+$/m, '')意味着删除字符串开头和字符串末尾的任何空格序列。

回答by sawa

input = <<X
     aa bbb
cc    dd ee
X

input.strip.split(/\s+/)

回答by vidang

input.split("\s")

If "\s"is used instead of /\s/, whites-paces will be removed from the result.

如果"\s"使用 代替/\s/,则将从结果中删除空格。

回答by J3RN

As a slight modification to Vidaica's answer, in Ruby 2.1.1 it looks like

作为对 Vidaica 答案的轻微修改,在 Ruby 2.1.1 中它看起来像

input.split(" ")

Will compensate for all whitespace, be it spaces, tabs, or newlines, yielding:

将补偿所有空格,无论是空格、制表符还是换行符,产生:

["aa", "bbb", "cc", "dd", "ee"]