Forum: Ruby on Rails Ruby string parsing

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.
0c89214676579ae34edec5bd3130d614?d=identicon&s=25 Nick (Guest)
on 2007-04-02 17:55
I'm fairly new to Ruby and I've got a question concerning string
parsing.  If I want to take a comma-delimited string of words and put
each word into an array, how would I do it?

Example:
string = "one, two, three, four"

Thanks!
D4d28bd014f9e7324bad99dcc3b0d390?d=identicon&s=25 Rich Morin (Guest)
on 2007-04-02 18:08
(Received via mailing list)
At 5:55 PM +0200 4/2/07, Nick wrote:
> I'm fairly new to Ruby and I've got a question concerning string
> parsing.  If I want to take a comma-delimited string of words and
> put each word into an array, how would I do it?
>
> Example:
> string = "one, two, three, four"

>> string.split(/,\s*/)
=> ["one", "two", "three", "four"]

-r

Note - Generic Ruby questions should probably be addressed to
       ruby-talk@ruby-lang.org
--
http://www.cfcl.com/rdm            Rich Morin
http://www.cfcl.com/rdm/resume     rdm@cfcl.com
http://www.cfcl.com/rdm/weblog     +1 650-873-7841

Technical editing and writing, programming, and web development
83ca41657a99b65d99889abe712ba5e2?d=identicon&s=25 Jason Roelofs (Guest)
on 2007-04-02 18:11
(Received via mailing list)
array = string.split(',')

or to make sure there's no extra white space (I think)

array = string.split(',\s*')

Please go read:

http://www.rubycentral.com/book/

and check out

http://ruby-doc.org/core

Jason
This topic is locked and can not be replied to.