Possible Duplicate:
Wrapping text into lines at word boundaries
I need to take a textarea input from a user and break it up into multiple lines every thirty chars. The hard part is I also need to make sure the line break occurs at a " "(space) in order to not break up words.
My initial attempt in trying to figure this out looked like this:
if @square.text.length > 30
text = @square.text[0, 31] + "\n" + @square.text[31, @square.text.length]
else
text = @square.text
end
Also, I know the above only handles a string that needs to be broken up once. I need up-to six line breaks.
Any ideas how to go about this?