将字符串拆分为指定大小的块,而不会破坏单词

时间:2021-02-18 17:51:35

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 个解决方案

#1


21  

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:

@sawa评论后更新:

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

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

这样更好,因为它不需要字符串以\ W结尾

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

它将处理超过指定长度的单词。实际上它会分裂它们,但我认为这是理想的行为

#2


5  

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

@Yuriy,你的轮换看起来很麻烦。怎么样:

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


21  

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:

@sawa评论后更新:

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

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

这样更好,因为它不需要字符串以\ W结尾

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

它将处理超过指定长度的单词。实际上它会分裂它们,但我认为这是理想的行为

#2


5  

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

@Yuriy,你的轮换看起来很麻烦。怎么样:

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)."]