Collins Richey
12/29/2007 7:41:00 PM
On Dec 29, 2007 12:11 PM, Mark Toth <mark.toth@telia.com> wrote:
>
> Brute force method? - What do you mean by this?
>
> ar = @string.split(' ')
> ar[1] = "<b>#{ar[1]}</b>"
> ar[3] = "<b>#{ar[3]}</b>"
> @string = ar.join(' ')
>
>
By brute force, I meant simply a quick and dirty operation
1. split the string into components
2. modify the components
3. reassemble/join the components
If you have need to do this on a repetitive basis, you could develop a
method to apply this logic, something like
def hilite(a_str, a_nbr, a_tag='b')
ix = a_nbr - 1
ar = a_str.split(' ')
ar[ix] = "<#{a_tag}>#{ar[ix]}</#{a_tag}>"
str = ar.join(' ')
return str
end
strx = "This is a long string"
p strx
strx = hilite(strx,2) # make word 2 bold
p strx
strx = hilite(strx,4,'i') # make word 4 italic
p strx
This is still pretty brute force, since the method will break badly if
you pass it the wrong elements - no error/bounds checking. You could
modify it to scan for a particular word to hilite instead of using a
word number.
Enjoy,
--
Collins Richey
If you fill your heart with regrets of yesterday and the worries
of tomorrow, you have no today to be thankful for.