In Ruby, you can split a string using the split
method.
In this example,
str
with a value of "Hello World"
.split
method with a space as the delimiter.str = 'Hello World'
tokens = str.split
puts tokens
Hello World
In this example,
str
with a value of "apple,banana,cherry"
.split
method with a comma as the delimiter.str = 'apple,banana,cherry'
tokens = str.split(',')
puts tokens
apple banana cherry
In this tutorial, we learned How to Split a String in Ruby language with well detailed examples.