Forum: Ruby convert string to array with delimiter

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Valentino L. (Guest)
on 2008-11-05 11:06
Dear all

I have the following string want to convert to arry

str="/dev/lis_home_712     2097152   1052656   1044496      51%
/appl/lis/home2\n"

I use str.scan(/\w+/), which give the following result
["dev", "lis_home_712", "2097152", "1052656", "1044496", "51", "appl",
"lis", "home2"]

However, I expect the result like this
["dev/lis_home_712", "2097152", "1052656", "1044496", "51%",
"/appl/lis/home2"]

Please advise on how to do it. Thank you.
Farrel L. (Guest)
on 2008-11-05 11:11
(Received via mailing list)
2008/11/5 Valentino L. <removed_email_address@domain.invalid>:
> I use str.scan(/\w+/), which give the following result

str.split( ' ' )

Farrel
Peña, Botp (Guest)
on 2008-11-05 11:14
(Received via mailing list)
From: Valentino L. [mailto:removed_email_address@domain.invalid]
# However, I expect the result like this
# ["dev/lis_home_712", "2097152", "1052656", "1044496", "51%",
# "/appl/lis/home2"]
#
# Please advise on how to do it. Thank you.

read on string fxns, qri String#.
here in this case you may use String#split

try first,

  str.split

(note w/out an arg)
Heesob P. (Guest)
on 2008-11-05 11:18
(Received via mailing list)
2008/11/5 Valentino L. <removed_email_address@domain.invalid>:
>
> However, I expect the result like this
> ["dev/lis_home_712", "2097152", "1052656", "1044496", "51%",
> "/appl/lis/home2"]
>
> Please advise on how to do it. Thank you.

str.scan(/\S+/)
or
str.split(/\s+/)
or
str.split

Regards,

Park H.
This topic is locked and can not be replied to.