On 12.03.2007 16:23, Kev wrote: > I have written a loop to basically parse a string, and at every 50th > character check to see if is a space, if not, work back until it > finds one, then insert a newline. I am turning masses of text (copy) > from a DB into images, and I just wanted to automate it, I was just > wondering if there are better ways of achieving what I am trying to > do. > > characterCount = 0 > positionCount = 0 > insertPoint = MAX_LINE_LENGTH > > while characterCount != copy.length > characterCount += 1 > positionCount += 1 > if positionCount == MAX_LINE_LENGTH > begin > characterCount -= 1 > insertPoint -= 1 > end until copy[characterCount].eql?(ASCII_SPACE) > copy.insert(characterCount+=1,'\n') > imageHeight += LINE_HEIGHT > positionCount = 0 > end > > end There are quite a lot of posts about word wrapping which seems what you are trying to do. You should be able to find them via the archives (Google Groups, ruby-talk archive). A simplistic approach would probably do something like this: str.gsub(/(.{1,50})\s+/, "\\1\n") Kind regards robert