0Pricing
Ruby Academy · Lesson

Splitting and Joining

split, join, chars.

Splitting a String

The split method breaks a string into an array using a separator. By default it splits on whitespace.

sentence = "the quick brown fox"
words = sentence.split
puts words.inspect

Splitting on a Delimiter

Pass any string as the separator, such as a comma for CSV-style data.

csv = "red,green,blue"
puts csv.split(",").inspect

All lessons in this course

  1. String Basics and Methods
  2. Interpolation and Formatting
  3. Searching and Replacing
  4. Splitting and Joining
← Back to Ruby Academy