Pranav Reddy Pranav Reddy - 1 year ago 66
Ruby Question

How to split on two string delimters

How would I split a string based on two string delimiters like

" of "
" in "

For example, the string:

"dogs in Toronto of Canada"

would transform into the array:

["dogs", "Toronto", "Canada"]

Answer Source

You can use String#split with regex:

"dogs in Toronto of Canada".split(/\sof\s|\sin\s/)
=> ["dogs", "Toronto", "Canada"]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download