ruby - Split sentence into words only using while loop -


in c++, can add null character in end differentiate between words. not how in ruby not want use splice method. here how proceeding forward with.

i = 0     split_array = []     while (i<sentence.length)         puts (sentence.length)         if (i+1 == " ") 

another way (disregarding punctuation , case):

sentence = "the quick brown dog\njumped   on lazy fox"  start_ndx = sentence.index(/\w/) return [] if start_ndx.nil? words = [] while start_ndx   end_ndx = sentence.index(/\w\b/, start_ndx)   words << sentence[start_ndx..end_ndx]   start_ndx = sentence.index(/\w/, end_ndx+1) end words   #=> ["the", "quick", "brown", "dog", "jumped", "over", "the", "lazy", "fox"]  

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -