From: Colin Bartlett Date: 2010-06-04T03:48:49+09:00 Subject: Re: Split text into equal characters but keeping whole words --00163691fb381cf00e048824a8e6 Content-Type: text/plain; charset=ISO-8859-1 On Thu, Jun 3, 2010 at 10:19 AM, John Butler wrote: > I have text that is pretty large. What i want to do is split it into an > array of no more than 250 characters per line but finishing on a full > word at the end of each line. So if the last word was "ruby" but 'b' > was the 250th character in the line then i want the line to cut off > before ruby and then start at ruby for the next line. ... Any ideas? > Using as an example the text below (setting to one side my slight surprise that Warren Buffett knows the quote by Jacobi), and ignoring any edge cases and subtle issues mentioned by Ian Hobson and David A Black, an idea might be: * go through the text adding 250 (or whatever) to the "latest" end position; * use rindex and David Black's suggestion of the \b anchor to search *backwards* for the "first" previous word boundary; * a refinement is use that as a "first guess" for the next split position, and then see if any edge cases or issues raised by Ian Hobson should make the split position "earlier". (But once you're doing that, it might perhaps be better to use Ian Hobson's suggestion of going forwards one word at a time and checking if it will fit, or if there is a paragraph break, etc.) def split_on_words_with_max_line_length( text, max_line_length ) text_last_index_plus_1 = text.length ii = nil ; jj = 0; aa = [] while true ii = jj; jj = ii + max_line_length if jj < text_last_index_plus_1 then ww = text.rindex( %r{\b}m, jj ) jj = ww # done like this in case jj needed for edge cases else jj = text_last_index_plus_1 end aa << text[ ii ... jj ].strip break unless jj < text_last_index_plus_1 end aa end text = "http://en.wikipedia.org/wiki/Carl_Gustav_Jacob_Jacobi" \ " Carl Gustav Jacob Jacobi (10 December 1804 - 18 February 1851)" \ " was a Prussian mathematician, widely considered to be" \ " the most inspiring teacher of his time and one of" \ " the greatest mathematicians of all time." \ " ... It was in algebraic development that Jacobi's peculiar power" \ " mainly lay, and he made important contributions of this kind" \ " to many areas of mathematics ... One of his maxims was:" \ " 'Invert, always invert' ('man muss immer umkehren')," \ " expressing his belief that the solution of many hard problems" \ " can be clarified by re-expressing them in inverse form." \ "\n\n" \ "http://www.ibtimes.com/articles/20100227/" \ "invert-always-invert-buffet-advises-shareholders.htm" \ " In his annual letter to shareholders, legendary investor" \ " Warren Buffett outlined a few approaches to investing" \ " and business management that should be avoided." \ " Buffett cited Jacobi, a mathematician, who advised problem solvers" \ " to \"invert, always invert\". In other words, instead of trying" \ " to find ways of doing something successfully, first find methods" \ " that are likely fail and avoid them." max_line_length = 102 # or whatever puts text puts; puts split_on_words_with_max_line_length( text, max_line_length ) --00163691fb381cf00e048824a8e6--