« Return to Thread: The easiest way to separate substrings from a line string

Re: The easiest way to separate substrings from a line string

by Robert Klemme-2 :: Rate this Message:

Reply to Author | View in Thread

2009/7/9 David A. Black <dblack@...>:

> Hi --
>
> On Thu, 9 Jul 2009, Sarawut Poaitwinyu wrote:
>
>> I have working on tag system and There is information like this
>>
>> "important urgent project 2009"
>>
>> i want to seperate into word, one word at the time is okay
>>
>> What is the easiest way to separate those string word by word, space
>> between each word might has more than 1.
>>
>> My idea is to use loop to check character by character that it is space
>> or not, and then cut the part, but i thought it might have easier way
>> that i don't know
>
>  words = string.split
>
> When you call split with no argument, it splits on whitespace
> (including more than one character).

I am more like the "positive" guy - meaning explicitly defining what I
want returned.  I would do

words = string.scan /\w+/

That way dot, question mark and other signs won't hurt.  It may not
make a difference but it's probably good to see different approaches.

Kind regards

robert


--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

 « Return to Thread: The easiest way to separate substrings from a line string