14

I need to split a string into chunks according to a specific size. I cannot break words between chunks, so I need to catch when adding the next word will go over chunk size and start the next one (it's ok if a chunk is less than specified size).

Here is my working code, but I would like to find a more elegant way to do this.

def split_into_chunks_by_size(chunk_size, string)
  string_split_into_chunks = [""]
  string.split(" ").each do |word|
    if (string_split_into_chunks[-1].length + 1 + word.length > chunk_size)
      string_split_into_chunks << word
    else
      string_split_into_chunks[-1] << " " + word
    end
  end
  return string_split_into_chunks
end

2 Answers 2

25

How about:

str = "split a string into chunks according to a specific size. Seems easy enough, but here is the catch: I cannot be breaking words between chunks, so I need to catch when adding the next word will go over chunk size and start the next one (its ok if a chunk is less than specified size)." 
str.scan(/.{1,25}\W/)
=> ["split a string into ", "chunks according to a ", "specific size. Seems easy ", "enough, but here is the ", "catch: I cannot be ", "breaking words between ", "chunks, so I need to ", "catch when adding the ", "next word will go over ", "chunk size and start the ", "next one (its ok if a ", "chunk is less than ", "specified size)."]

Update after @sawa comment:

str.scan(/.{1,25}\b|.{1,25}/).map(&:strip)

This is better as it doesn't require a string to end with \W

And it will handle words longer than specified length. Actually it will split them, but I assume this is desired behaviour

4
  • works great, thanks a lot! one more thing: can we trim trailing spaces right in here? Mar 14, 2013 at 5:23
  • 1
    This is close to good, but it always requires a \W character at the end. In your particular example, it worked because of the ) and . at the end, but without it, it won't work. Each chunk also necessarily ends with a \W character when it does not have to.
    – sawa
    Mar 14, 2013 at 6:20
  • Thank you for pointing this out. Updated the answer to solve this problems Mar 14, 2013 at 7:10
  • 1
    I ended up using /.{1,25}\z|.{1,25}\s|.{1,25}/. The /.{1,25}\z being a "catch all" at the end of the string.
    – XML Slayer
    Apr 17, 2018 at 15:49
5

@Yuriy, your alternation looks like trouble. How about:

str.scan /\S.{1,24}(?!\S)/
#=> ["split a string into", "chunks according to a", "specific size. Seems easy", "enough, but here is the", "catch: I cannot be", "breaking words between", "chunks, so I need to", "catch when adding the", "next word will go over", "chunk size and Start the", "next one (its ok if a", "chunk is less than", "specified size)."]
1
  • Yeah, this seems better, but it will truncate words longer than 25 symbols. Mar 19, 2013 at 14:41

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.