In Ruby, you can split a string by new line using the split
method with the newline character '\n'
.
In this example,
str
with a value containing new lines.split
method with the newline character '\n'
to split the string into an array of substrings.str = "Hello\nWorld\n"
substrings = str.split("\n")
substrings.each do |sub|
puts sub
end
Hello World
In this tutorial, we learned How to Split a String by New Line in Ruby language with well detailed examples.