What is the best way to chop a string into chunks of a given length in Ruby?

前端 未结 9 2081
陌清茗
陌清茗 2020-12-12 18:40

I have been looking for an elegant and efficient way to chunk a string into substrings of a given length in Ruby.

So far, the best I could come up with is this:

9条回答
  •  天命终不由人
    2020-12-12 19:07

    Are there some other constraints you have in mind? Otherwise I'd be awfully tempted to do something simple like

    [0..10].each {
       str[(i*w),w]
    }
    

提交回复
热议问题