From: Phrogz Date: 2007-02-02T15:30:05+09:00 Subject: Re: halving a string Chris Shea wrote: > I want to make adding new fortunes easy (and without having to figure > out myself where to force a line break), so I basically need to split > a string (approximately) in half at a word boundary. This is what I > have: [snip[ > I have a feeling there's a one-line regexp that can do this. Am I > right? If not, is there a better way? This is what I would do: strings = DATA.read.split( /\n/ ) strings.each{ |str| puts str.gsub( /^(.{#{str.length/2},}?)\s(.+)/ ){ "#{$1}\n#{$2}" } puts } #=> Hello #=> World #=> It's the end of the #=> world as we know it #=> If you didn't know any better #=> you'd think this was magic. __END__ Hello World It's the end of the world as we know it If you didn't know any better you'd think this was magic.