Re: Wrapping strings



Ben <benbelly@xxxxxxxxx> writes:

>I have a string containing multiple lines that I would like to put
>into a C++ file as a comment:
>desc =3D ""
>while (line =3D gets) !~ /^\.$/
> desc +=3D "#{line}"
>end

>I'd like to precede each line with a "// " to make it a comment, and
>I'd like to wrap each line by the 78th character.

>I don't suppose there is any way to do this quick and simple like?

>I figure I need to pull out the carriage returns, but then I can't
>think of anything pretty after that. Any tips?

I recommend getting the text-format module (available from the RAA
at http://rubyforge.org/projects/text-format or as a gem) and using it.

If that doesn't fit your needs for some reason, you can do it manually.
Something like this should work:

words = line.split(/\s+/)
while words do
line = '// '
while words && line.length < 78 do
line += words.shift + ' '
end
puts line
end




>-Ben


.



Relevant Pages

  • Re: text justification
    ... ** The wordwrap function splits the input string at whitespace ... ** which is at the wrap position specified or less. ... ** string just after the last character that was copied to the ... char *wordwrap( ...
    (comp.lang.c)
  • Re: FileReader / BufferedReader Help
    ... If you can have mixed CR LF and LF only in the same file, you'll have to be certain that this happens in the right order or you may read multiple blank lines as a single line. ... But also an int can just be cast to a char. ... Use StringBuffer to build up the string one character at a time. ...
    (comp.lang.java.help)
  • Re: How to Wrap the text
    ... the string using Printer Object Printer.Print. ... to set the display length to 20 character. ... I do not need to wrap with space, I just want it to wrap ... WrapAtX = WrapAtX & Mid& vbCrLf ...
    (microsoft.public.vb.general.discussion)
  • Wrapping strings
    ... I have a string containing multiple lines that I would like to put ... I'd like to wrap each line by the 78th character. ... Prev by Date: ...
    (comp.lang.ruby)
  • Re: How to Wrap the text
    ... the string using Printer Object Printer.Print. ... to set the display length to 20 character. ... I do not need to wrap with space, I just want it to wrap ... code by using the textwidth property and loop like the previous post. ...
    (microsoft.public.vb.general.discussion)