Split string by multiple delimiters
RubyStringSplitRuby Problem Overview
I want to split a string by whitespaces, ,
and '
using a single ruby command.
-
word.split
will split by white spaces; -
word.split(",")
will split by,
; -
word.split("\'")
will split by'
.
How to do all three at once?
Ruby Solutions
Solution 1 - Ruby
word = "Now is the,time for'all good people"
word.split(/[\s,']/)
=> ["Now", "is", "the", "time", "for", "all", "good", "people"]
Solution 2 - Ruby
Regex.
"a,b'c d".split /\s|'|,/
# => ["a", "b", "c", "d"]
Solution 3 - Ruby
You can use a combination of the split
method and the Regexp.union
method like so:
delimiters = [',', ' ', "'"]
word.split(Regexp.union(delimiters))
# => ["Now", "is", "the", "time", "for", "all", "good", "people"]
You can even use regex patters in the delimiters.
delimiters = [',', /\s/, "'"]
word.split(Regexp.union(delimiters))
# => ["Now", "is", "the", "time", "for", "all", "good", "people"]
This solution has the advantage of allowing totally dynamic delimiters or any length.
Solution 4 - Ruby
Here is another one :
word = "Now is the,time for'all good people"
word.scan(/\w+/)
# => ["Now", "is", "the", "time", "for", "all", "good", "people"]
Solution 5 - Ruby
x = "one,two, three four"
new_array = x.gsub(/,|'/, " ").split
Solution 6 - Ruby
I know this is an old thread but I just happened to stumble on it and thought I would leave another answer. I personally like to avoid using regex
, both because I find it hard to read and because its almost always slower than using other built in methods. So, instead of the aforementioned regex solutions, I would also consider using the following:
word.gsub(",", " ").gsub("'", " ").split
The first gsub
replaces all occurrences of ,
with a space
. The second gsub replaces all occurrences of '
with a space
. This results in whitespace
at all the desired locations. And then split
with no argument simply splits on whitespace.
Its only slightly faster than some of the aforementioned solutions, but I do believe it is faster than any others that have been mentioned.