Ruby Language Strings Splitting a String

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

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"] 


Got any Ruby Language Question?