[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

splitting a string separated by CRLF into array

dominique.plante@gmail.com

7/21/2006 8:22:00 PM

How can I get the array

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

from the string

"hello\r\nworld\r\n123"

Thanks!
Dominique

4 Answers

dblack

7/21/2006 8:29:00 PM

0

dominique.plante@gmail.com

7/21/2006 9:11:00 PM

0

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




dblack@wobblini.net wrote:
> Hi --
>
> On Sat, 22 Jul 2006, dominique.plante@gmail.com wrote:
>
> > How can I get the array
> >
> > ["hello", "world", "123"]
> >
> > from the string
> >
> > "hello\r\nworld\r\n123"
>
> str.split("\r\n")
>
>
> David
>
> --
> http://www.rubypoweran... => Ruby/Rails training & consultancy
> http://www.manning... => RUBY FOR RAILS (reviewed on
> Slashdot, 7/12/2006!)
> http://dablog.r... => D[avid ]A[. ]B[lack's][ Web]log
> dblack@wobblini.net => me

William James

7/21/2006 9:13:00 PM

0

dominique.plante@gmail.com 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"]

Tom Werner

7/21/2006 9:20:00 PM

0

dominique.plante@gmail.com 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

--
Tom Werner
Helmets to Hardhats
Software Developer
tom@helmetstohardhats.org
www.helmetstohardhats.org