How to split a delimited string in Ruby and convert it to an array?
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:
arr
-> string which you're going to perform any action.split()
-> is an method, which split the input and store it as array.''
or' '
or','
-> is an value, which is needed to be removed from given string.