Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 172 Vote(s) - 3.72 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to split a delimited string in Ruby and convert it to an array?

#1
I have a string

`"1,2,3,4"`

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

[1,2,3,4]

How?
Reply

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

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

#3
>> "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):

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

#4
### 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. `split()` -> is an method, which split the input and store it as array.
3. `''` or `' '` or `','` -> is an value, which is needed to be removed from given string.
Reply

#5
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][1]


> Divides str into substrings based on a delimiter, returning an array
> of these substrings.
>
> 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.
>
> 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.
>
> 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.
>
> 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]:

[To see links please register here]

Reply

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

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

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



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through