Using more than one delimiter for .split method in Ruby

I want to split a string into an array using ";"

, " "

and ","

delimiters on one line. Is there a way to do this without using regular expressions?

+3


source to share


2 answers


Just replace the delimiters with one delimiter and divide by that.



p "aaa;bbb ccc,ddd".tr(";,"," ").split  #=> ["aaa", "bbb", "ccc", "ddd"]

      

+4


source


Yes. Perhaps, but with a regular expression.



string.split(/[;, ]/)

      

+7


source







All Articles