'Split string terms
I have the following string:
str = "peep toe t-shirt blue"
I need to split the items using space:
str.split(" ")
And I get
[pee, toe, t-shirt, blue]
The questions is that peep toe must not be splitted, because it's a name that makes sense together. Is there a way to solve this?
Solution 1:[1]
I think it's good to use split and then fix up the data later:
str = "peep toe t-shirt blue"
words = str.split(" ")
phrases = []
i = 0
while i < words.size
if words[i] == 'peep' && words[i + 1] == 'toe'
phrases << 'peep toe'
i += 2
else
phrases << words[i]
i += 1
end
end
p phrases
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | David Grayson |
