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
Dimitry_N
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
steenslag
source
to share
Yes. Perhaps, but with a regular expression.
string.split(/[;, ]/)
+7
Arup Rakshit
source
to share