Re: String Search and Replace

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

From: Jay Daulton <JayD at ADVANCEDBIONICS.COM>

Subject: String Search and Replace

> Can someone post a string search and replace function?
> I think that Euphoria will benchmark quite a bit better than
> what I am currently using in J.

Take a look at the match() function.
Also, you need to decide whether to replace only the first instance,
and return, or whether you want to replace all instances without
asking. Below is one possibility:

sequence t
t = "Now is the time for all good men to come to the aid of the party"

function Replace(sequence target, sequence old, sequence new)
integer i,j
 i = match(old,target)
 if i > 0 then
    j = i + length(old)
    return target[1..i-1] & new & target[j..length(target)]
 else
  return target
 end if
end function

puts(1,Replace(t,"men","women"))

Regards,
Irv

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

Search



Quick Links

User menu

Not signed in.

Misc Menu