From: Joao Pedrosa Date: 2004-10-12T00:09:42+09:00 Subject: Re: string replacement Hi, On Mon, 11 Oct 2004 11:22:52 -0300, Joao Pedrosa wrote: > Hi, > > On Mon, 11 Oct 2004 23:06:58 +0900, J.Lang wrote: > > we need to replace a whitespace with a
if the string is longer > > then 30 chars (next whitespace after 30 chars) > > > > does someone have a idea to programm this in ruby? Here is my second take: 5.times{ s = 'a' * (rand * 100) p "s (#{s.size}): #{s}" z = '' while s do if s.size <= 30 z += s break else i = s[30..-1].index(' ') || s.size i += 30 z += s[0..i] s = s[i+1..-1] if s z += '
' end end end p '---z---', z, '-------' } This time I break a string that is bigger than 30 characters into small chunks of 30 or so characters, depending on the next blank space, which is substituted by a
tag. If the string is big enough and has enough spaces, then it may be broken in as many necessary chunks of 30 or so characters. Is that it? Cheers, Joao