10

I have a string that I'm using .split(' ') on to split up the string into an array of words. Can I use a similar method to split the string into an array of 2 words instead?

Returns an array where each element is one word:

words = string.split(' ')

I'm looking to return an array where each element is 2 words instead.

3
  • How do you want to treat an odd number of words? Drop the last word? Drop the first word? Keep it? Commented Apr 10, 2013 at 0:16
  • possible duplicate of Need to split arrays to sub arrays of specified size in Ruby Commented Apr 10, 2013 at 0:45
  • 1
    Markthomas did you even read the question Commented Apr 10, 2013 at 1:40

5 Answers 5

7
str = 'one two three four five six seven'
str.split.each_slice(2).map{|a|a.join ' '}
=> ["one two", "three four", "five six", "seven"]

This also handles the case of an odd number of words.

Sign up to request clarification or add additional context in comments.

3 Comments

I'm trying to figure out how I can reduce this to something like str.split.each_slice(2).map(&:join) but I can't get the ' ' argument on the join.
Joe: It's better to open a separate question. Hint: not &: but instead inline {|t|t.join(' ')}
Thank you for the comment @SimonB. but the comment was mostly my own comment to myself about my own answer. The answer reflects your hint already, I was trying to make this more terse.
4

You can do

string= 'one1! two2@ three3# four4$ five5% six6^ sev'
string.scan(/\S+ ?\S*/)
# => ["one1! two2@", "three3# four4$", "five5% six6^", "sev"]

4 Comments

Not if the last word is a single character.
is you replace the ` ?` with \s*, then it will also preserve the original whitespace between the words.
@JoshuaCheek Sorry I don't get it. What do you mean by the original whitespace?
@oldergod if it was "one1!\t two2@", this would retain the tab and extra spaces (SO is only displaying one space in the example, but pretend there were many)
3

Something like this should work:

string.scan(/\w+ \w+/)

7 Comments

str = 'one1! two2@ three3# four4$ five5% six6^' outputs []. I don't know if the words are all alphabetic
maybe string.scan(/\S+ \S+/) would be better.
It's also unanswered how to tread an odd number of words. I dunno.
Probably /\S+\s+\S+/ because you don't necessarily know that it's one space between words.
OP isn't responding, it could be that they literally want to split on spaces. Who knows... I'm not spending more time on this.
|
2

Ruby's scan is useful for this:

'a b c'.scan(/\w+(?:\s+\w+)?/)
=> ["a b", "c"]

'a b c d e f g'.scan(/\w+(?:\s+\w+)?/)
=> ["a b", "c d", "e f", "g"]

Comments

2

This is all I had to do:

def first_word
    chat = "I love Ruby"
    chat = chat.split(" ")
    chat[0]
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.