splitting a string separated by CRLF into array

D

dominique.plante

How can I get the array

["hello", "world", "123"]

from the string

"hello\r\nworld\r\n123"

Thanks!
Dominique
 
D

dominique.plante

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"]




Hi --

How can I get the array

["hello", "world", "123"]

from the string

"hello\r\nworld\r\n123"

str.split("\r\n")


David

--
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
http://www.manning.com/black => RUBY FOR RAILS (reviewed on
Slashdot, 7/12/2006!)
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
(e-mail address removed) => me
 
W

William James

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"]
 
T

Tom Werner

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

--
Tom Werner
Helmets to Hardhats
Software Developer
(e-mail address removed)
www.helmetstohardhats.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top