Ad space (banner)
๐Ÿ’ŽRuby Lessons
Lesson 20 / 68

Splitting and Joining Strings (split, join)

In this lesson you will learn to split and join strings in Ruby so you can reshape text data such as CSV at will. This is for anyone searching for "Ruby split join how to use".

.split(delimiter) breaks a string into an array. Going the other way, .join(delimiter) collects an array into a single string. Combining the two lets you swap delimiters, strip out unwanted spaces, and generally reshape text at will.

The sample code splits the comma-separated csv into the array fruits with csv.split(","), then rebuilds it with a different delimiter using fruits.join(" / ").

A common stumbling block for beginners is which class each method belongs to. Keeping in mind that split is a String method and join is an Array method makes the code much easier to read and write.

This pairing appears constantly in real business systems, from reading CSV files to parsing logs. These two methods come into play wherever text data is handled.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Ruby directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)