Ruby Language Splitting a String


Example

String#split splits a String into an Array, based on a delimiter.

"alpha,beta".split(",")
# => ["alpha", "beta"]

An empty String results into an empty Array:

"".split(",")
# => []

A non-matching delimiter results in an Array containing a single item:

"alpha,beta".split(".")
# => ["alpha,beta"]

You can also split a string using regular expressions:

"alpha, beta,gamma".split(/, ?/)
# => ["alpha", "beta", "gamma"]

The delimiter is optional, by default a string is split on whitespace:

"alpha beta".split
# => ["alpha", "beta"]