如何在Ruby中分割一个分隔的字符串并将其转换成一个数组?

时间:2021-12-03 21:43:54

I have a string

我有一个字符串

"1,2,3,4"

“1、2、3、4”

and I'd like to convert it into an array:

我想把它转换成一个数组:

[1,2,3,4]

How?

如何?

5 个解决方案

#1


360  

>> "1,2,3,4".split(",")
=> ["1", "2", "3", "4"]

Or for integers:

或为整数:

>> "1,2,3,4".split(",").map { |s| s.to_i }
=> [1, 2, 3, 4]

Or for later versions of ruby (>= 1.9 - as pointed out by Alex):

或者是ruby的后续版本(>= 1.9——由Alex指出):

>> "1,2,3,4".split(",").map(&:to_i)
=> [1, 2, 3, 4]

#2


30  

"1,2,3,4".split(",") as strings

“1、2、3、4”.split(",")的字符串

"1,2,3,4".split(",").map { |s| s.to_i } as integers

“1、2、3、4”.split(",")。{ | |年代地图。to_i }为整数

#3


12  

For String Integer without space as String

arr = "12345"

arr.split('')

output: ["1","2","3","4","5"]

For String Integer with space as String

arr = "1 2 3 4 5"

arr.split(' ')

output: ["1","2","3","4","5"]

For String Integer without space as Integer

arr = "12345"

arr.split('').map(&:to_i)

output: [1,2,3,4,5]

For String

arr = "abc"

arr.split('')

output: ["a","b","c"]

Explanation:

解释:

  1. arr -> string which you're going to perform any action.
  2. arr ->字符串,你将执行任何动作。
  3. split() -> is an method, which split the input and store it as array.
  4. split() ->是一种方法,它将输入拆分并将其存储为数组。
  5. '' or ' ' or ',' -> is an value, which is needed to be removed from given string.
  6. 或''或',' ->是一个值,需要从给定的字符串中删除。

#4


1  

the simplest way to convert a string that has a delimiter like a comma is just to use the split method

最简单的方法是使用分割方法来转换具有分隔符(例如逗号)的字符串。

"1,2,3,4".split(',') # "1", "2", "3", "4"]

you can find more info on how to use the split method in the ruby docs

您可以在ruby文档中找到更多关于如何使用split方法的信息。

Divides str into substrings based on a delimiter, returning an array of these substrings.

根据分隔符将str分为子字符串,返回这些子字符串的数组。

If pattern is a String, then its contents are used as the delimiter when splitting str. If pattern is a single space, str is split on whitespace, with leading whitespace and runs of contiguous whitespace characters ignored.

如果模式是一个字符串,那么它的内容在拆分str时被用作分隔符。如果模式是一个单独的空间,那么str在空格处被分割,而空白的空格和连续的空白字符将被忽略。

If pattern is a Regexp, str is divided where the pattern matches. Whenever the pattern matches a zero-length string, str is split into individual characters. If pattern contains groups, the respective matches will be returned in the array as well.

如果模式是Regexp,则str被划分为模式匹配的位置。每当模式匹配一个零长度的字符串时,str就被分解为单个字符。如果模式包含组,则将在数组中返回相应的匹配项。

If pattern is omitted, the value of $; is used. If $; is nil (which is the default), str is split on whitespace as if ` ‘ were specified.

如果模式被省略,$的值;使用。如果美元;是nil(默认情况下),str在空格处拆分,就好像“”被指定了一样。

If the limit parameter is omitted, trailing null fields are suppressed. If limit is a positive number, at most that number of fields will be returned (if limit is 1, the entire string is returned as the only entry in an array). If negative, there is no limit to the number of fields returned, and trailing null fields are not suppressed.

如果省略了限制参数,则会抑制尾随的空字段。如果限制是一个正数,那么最多将返回字段的数量(如果限制为1,则返回整个字符串作为数组中唯一的条目)。如果是负的,则返回的字段的数量没有限制,并且尾随的空字段没有被抑制。

#5


0  

"12345".each_char.map(&:to_i)

each_char does basically the same as split(''): It splits a string into an array of its characters.

each_char与split()基本相同:它将一个字符串分割为一个字符数组。

hmmm, I just realize now that in the original question the string contains commas, so my answer is not really helpful ;-(..

嗯,我现在才意识到在原来的问题中,字符串包含逗号,所以我的答案并不是很有帮助。

#1


360  

>> "1,2,3,4".split(",")
=> ["1", "2", "3", "4"]

Or for integers:

或为整数:

>> "1,2,3,4".split(",").map { |s| s.to_i }
=> [1, 2, 3, 4]

Or for later versions of ruby (>= 1.9 - as pointed out by Alex):

或者是ruby的后续版本(>= 1.9——由Alex指出):

>> "1,2,3,4".split(",").map(&:to_i)
=> [1, 2, 3, 4]

#2


30  

"1,2,3,4".split(",") as strings

“1、2、3、4”.split(",")的字符串

"1,2,3,4".split(",").map { |s| s.to_i } as integers

“1、2、3、4”.split(",")。{ | |年代地图。to_i }为整数

#3


12  

For String Integer without space as String

arr = "12345"

arr.split('')

output: ["1","2","3","4","5"]

For String Integer with space as String

arr = "1 2 3 4 5"

arr.split(' ')

output: ["1","2","3","4","5"]

For String Integer without space as Integer

arr = "12345"

arr.split('').map(&:to_i)

output: [1,2,3,4,5]

For String

arr = "abc"

arr.split('')

output: ["a","b","c"]

Explanation:

解释:

  1. arr -> string which you're going to perform any action.
  2. arr ->字符串,你将执行任何动作。
  3. split() -> is an method, which split the input and store it as array.
  4. split() ->是一种方法,它将输入拆分并将其存储为数组。
  5. '' or ' ' or ',' -> is an value, which is needed to be removed from given string.
  6. 或''或',' ->是一个值,需要从给定的字符串中删除。

#4


1  

the simplest way to convert a string that has a delimiter like a comma is just to use the split method

最简单的方法是使用分割方法来转换具有分隔符(例如逗号)的字符串。

"1,2,3,4".split(',') # "1", "2", "3", "4"]

you can find more info on how to use the split method in the ruby docs

您可以在ruby文档中找到更多关于如何使用split方法的信息。

Divides str into substrings based on a delimiter, returning an array of these substrings.

根据分隔符将str分为子字符串,返回这些子字符串的数组。

If pattern is a String, then its contents are used as the delimiter when splitting str. If pattern is a single space, str is split on whitespace, with leading whitespace and runs of contiguous whitespace characters ignored.

如果模式是一个字符串,那么它的内容在拆分str时被用作分隔符。如果模式是一个单独的空间,那么str在空格处被分割,而空白的空格和连续的空白字符将被忽略。

If pattern is a Regexp, str is divided where the pattern matches. Whenever the pattern matches a zero-length string, str is split into individual characters. If pattern contains groups, the respective matches will be returned in the array as well.

如果模式是Regexp,则str被划分为模式匹配的位置。每当模式匹配一个零长度的字符串时,str就被分解为单个字符。如果模式包含组,则将在数组中返回相应的匹配项。

If pattern is omitted, the value of $; is used. If $; is nil (which is the default), str is split on whitespace as if ` ‘ were specified.

如果模式被省略,$的值;使用。如果美元;是nil(默认情况下),str在空格处拆分,就好像“”被指定了一样。

If the limit parameter is omitted, trailing null fields are suppressed. If limit is a positive number, at most that number of fields will be returned (if limit is 1, the entire string is returned as the only entry in an array). If negative, there is no limit to the number of fields returned, and trailing null fields are not suppressed.

如果省略了限制参数,则会抑制尾随的空字段。如果限制是一个正数,那么最多将返回字段的数量(如果限制为1,则返回整个字符串作为数组中唯一的条目)。如果是负的,则返回的字段的数量没有限制,并且尾随的空字段没有被抑制。

#5


0  

"12345".each_char.map(&:to_i)

each_char does basically the same as split(''): It splits a string into an array of its characters.

each_char与split()基本相同:它将一个字符串分割为一个字符数组。

hmmm, I just realize now that in the original question the string contains commas, so my answer is not really helpful ;-(..

嗯,我现在才意识到在原来的问题中,字符串包含逗号,所以我的答案并不是很有帮助。