RE: Replacing characters

new topic     » goto parent     » topic index » view thread      » older message » newer message

> -----Original Message-----
> From: lists at wordit.com [mailto:lists at wordit.com]
> 
> How do you search and replace characters in a  sequence?
> 
> I need to replace \ with \\ and vice versa.
> 
> Thanks,
> 
> Marcus

This came up about a month or so ago.  Here's what I use (tested):

-- replaces all instances of a with b in text
function replace_all( sequence text, object a, object b )
    integer ix, jx
    
    if atom(a) then
        a = {a}
    end if
    
    if atom(b) then
        b = {b}
    end if
    
    ix = 0
    jx = match( a, text )
    while jx do
        ix += jx 
        text = text[1..ix-1] & b & text[ix+length(a)..length(text)]
        ix += length(b)
        jx = match( a, text[ix+length(a)..length(text)] )
    end while
    
    return text
    
end function

Matt Lewis

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu