[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

string replacement

J.Lang

10/11/2004 2:07:00 PM

we need to replace a whitespace with a <br> if the string is longer
then 30 chars (next whitespace after 30 chars)

does someone have a idea to programm this in ruby?


cheers
mike and john
6 Answers

Joao Pedrosa

10/11/2004 2:23:00 PM

0

Hi,

On Mon, 11 Oct 2004 23:06:58 +0900, J.Lang <jlang@ywesee.com> wrote:
> we need to replace a whitespace with a <br> if the string is longer
> then 30 chars (next whitespace after 30 chars)
>
> does someone have a idea to programm this in ruby?

Maybe:

5.times{
s = ' ' * (rand * 50)
len = s.size
z = s[30..-1]
if z
s = s[0..29] + z.gsub(' ', '<br />')
end

p "s (#{len}): #{s}", '---', "z: #{z}", '---'
}

Cheers,
Joao


Brian Schröder

10/11/2004 2:24:00 PM

0

J.Lang wrote:
> we need to replace a whitespace with a <br> if the string is longer
> then 30 chars (next whitespace after 30 chars)
>
> does someone have a idea to programm this in ruby?
>
>
> cheers
> mike and john
>
string = "abcd " * 30
string.gsub /(.{30}\S)\s/, '\1<br>'

regards,

Brian
--
Brian Schröder
http://ruby.brian-sch...


Robert Klemme

10/11/2004 2:31:00 PM

0


"J.Lang" <jlang@ywesee.com> schrieb im Newsbeitrag
news:1097503642.10233.38.camel@tux...
> we need to replace a whitespace with a <br> if the string is longer
> then 30 chars (next whitespace after 30 chars)

So you don't want to replace all spaces but only insert a line break every
~30 characters.

> does someone have a idea to programm this in ruby?

How about this:

str.gsub(/(.{30})\s+/, '\\1<br>')

Alternatively you can use gsub! if you want to modify the string in place.

Kind regards

robert

Ara.T.Howard

10/11/2004 3:01:00 PM

0

Joao Pedrosa

10/11/2004 3:10:00 PM

0

Hi,

On Mon, 11 Oct 2004 11:22:52 -0300, Joao Pedrosa <joaopedrosa@gmail.com> wrote:
> Hi,
>
> On Mon, 11 Oct 2004 23:06:58 +0900, J.Lang <jlang@ywesee.com> wrote:
> > we need to replace a whitespace with a <br> 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 += '<br />'
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 <br /> 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


Joao Pedrosa

10/11/2004 3:19:00 PM

0

Hi,

On Mon, 11 Oct 2004 12:09:39 -0300, Joao Pedrosa <joaopedrosa@gmail.com> wrote:
> Hi,
>
> On Mon, 11 Oct 2004 11:22:52 -0300, Joao Pedrosa <joaopedrosa@gmail.com> wrote:
> > Hi,
> >
> > On Mon, 11 Oct 2004 23:06:58 +0900, J.Lang <jlang@ywesee.com> wrote:
> > > we need to replace a whitespace with a <br> if the string is longer
> > > then 30 chars (next whitespace after 30 chars)
> > >
> > > does someone have a idea to programm this in ruby?

My last try, as I had forgotten to adjust the example. I changed the
creation of the sample string and made sure the space is substituted
correctly:

5.times{
s = '0123456789 ' * (rand * 10)
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 += '<br />'
end
end
end
p '---z---', z, '-------'
}

Cheers,
Joao