I have a ruby file that reads files and splits the text into an array using split(' ')
. The problem is that these text files contain newline characters, and I would like to retain those newline characters. For example, if I run the following code
"Lorem ipsum\ndolor sit amet".split(' ')
I get the output of
["Lorem", "ipsum", "dolor", "sit", "amet"]
Why does split remove the newline character? How can i retain \n
in my array?
Michael Berkowski's comment on your question is correct.
If you want to work around this case, use a regular expression:
"Lorem ipsum\ndolor sit amet".split(/ /)
#=> ["Lorem", "ipsum\ndolor", "sit", "amet"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With