DEV Community

Annie Huynh
Annie Huynh

Posted on

The .split Method

The .split method is a built-in method; it takes in a string and returns an array.

If we pass it a bit of text in parentheses, .split will divide the string wherever it sees that bit of text.

For example:

text. split(",")
Enter fullscreen mode Exit fullscreen mode

This code tells Ruby to split up the string text wherever it sees a comma.

For example:

text = "cat chased the mouse"
text.split(" ")
Enter fullscreen mode Exit fullscreen mode

output: ["cat", "chased", "the", "mouse"]

Top comments (0)