string replacement

J

J.Lang

--=-n45ceapFh3FUjFYpze9/
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

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

--=-n45ceapFh3FUjFYpze9/--
 
J

Joao Pedrosa

Hi,

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
 
B

Brian Schröder

J.Lang said:
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
 
R

Robert Klemme

J.Lang said:
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
 
A

Ara.T.Howard

--=-n45ceapFh3FUjFYpze9/
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

we need to replace a whitespace with a <br> 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 = '...<br>foo<br>bar<br>42<br>'

or

s = '...<br>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 << "<br>\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<br>
seven eight nine ten eleven<br>
twelve thirteen fourteen<br>
fifteen sixteen seventeen<br>
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
===============================================================================
 
J

Joao Pedrosa

Hi,


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
 
J

Joao Pedrosa

Hi,


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,417
Latest member
DarrenGaun

Latest Threads

Top