Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Francis Fish http://francis.blog-city.com

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Very simple ruby code to chunk a string

I wanted to chunk a string and saw lots of 100 line examples. This is the trivial thing I came up with. Does have a small bug when the string length is an exact multiple of the len variable (as in this example!) that adds an extra empty string on the end.

But it worked for what I needed.
   1  
   2    string = "some long test string or other"
   3    r = []
   4    len = 15
   5    start = 0
   6    while(start <= string.length) do                                              
   7      r << string[start...start+len] 
   8      start += len 
   9    end
  10    r # ["some long test ", "string or other", ""]
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS