From: Ara.T.Howard@...
Date: 2004-10-12T00:14:44+09:00
Subject: Re: string replacement
On Mon, 11 Oct 2004, J.Lang wrote:
> --=-n45ceapFh3FUjFYpze9/
> Content-Type: text/plain
> Content-Transfer-Encoding: 7bit
>
> we need to replace a whitespace with a
if the string is longer
> then 30 chars (next whitespace after 30 chars)
all whitespace after 30 chars, or every whitespace after 30 chars?
eg. if
s = '... foo bar 42 '
^
^
30
do you want
s = '...
foo
bar
42
'
or
s = '...
foo bar 42 '
are you trying to create a string buffer that will wrap after 30 chars?
if so maybe something like this.
harp:~ > cat a.rb
def brwrap buf, opts = {'width' => 30}
width = Integer opts['width']
wrapped = ['']
buf.scan(%r/\S+/) do |token|
line = wrapped.last
unless line.size + token.size + 1 < width
line << "
\n"
wrapped << ''
line = wrapped.last
end
line << ' ' << token
end
wrapped.join
end
buf = <<-txt
one two three four five six seven
eight nine ten eleven twelve thirteen
fourteen fifteen sixteen seventeen eightteen
txt
puts(brwrap(buf))
harp:~ > ruby a.rb
one two three four five six
seven eight nine ten eleven
twelve thirteen fourteen
fifteen sixteen seventeen
eightteen
of course anything like this will fail if the buffer contains lines which have
no spaces AND are more than 30 chars long!
regards.
-a
--
===============================================================================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| A flower falls, even though we love it;
| and a weed grows, even though we do not love it.
| --Dogen
===============================================================================