Splitting a string separated by CRLF into array

How can I get the array

[“hello”, “world”, “123”]

from the string

“hello\r\nworld\r\n123”

Thanks!
Dominique

Hi –

On Sat, 22 Jul 2006, [email protected] wrote:

How can I get the array

[“hello”, “world”, “123”]

from the string

“hello\r\nworld\r\n123”

str.split("\r\n")

David

Thanks! (I like your book BTW - I like how it touches on main, Kernel
stuff, etc…)

I was trying “hello\r\n\world”.split(’\r\n’) and it returned
[“hello\r\nworld”]

[email protected] wrote:

Thanks! (I like your book BTW - I like how it touches on main, Kernel
stuff, etc…)

I was trying “hello\r\n\world”.split(’\r\n’) and it returned
[“hello\r\nworld”]

You need to use a double quoted string in order for those escaped
characters to be interpereted as such.

Tom

[email protected] wrote:

How can I get the array

[“hello”, “world”, “123”]

from the string

“hello\r\nworld\r\n123”

Thanks!
Dominique

irb(main):001:0> “hello\r\nworld\r\n123”.split
=> [“hello”, “world”, “123”]

irb(main):011:0> “hello\r\nworld\r\n123”.split(/\s+/)
=> [“hello”, “world”, “123”]

irb(main):012:0> “hello\r\nworld\r\n123”.scan(/\S+/)
=> [“hello”, “world”, “123”]

irb(main):004:0> “hello\r\nworld\r\n123”.to_a.map{|x|x.chomp}
=> [“hello”, “world”, “123”]

irb(main):009:0> “hello\r\nworld\r\n123”.inject([]){|a,b| a << b.chomp}
=> [“hello”, “world”, “123”]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs