Re: String Search and Replace

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

I need to replace all matches without asking. Is there a library (maybe at
someone's website) of commonly used functions like search and replace,
different random number generators, probability distributions, misc. string
functions? In the C++ world they have what they like to consider "generic"
programming which makes use of standard template libraries - like SGI's.
There are literally hundreds of utility functions for use on what they call
"containers". There are about 30 of those ranging from bags to sets to lists
to arrays..... In Euphoria's case fortunately there is only one container
called a sequence. Most of the C++ compilers ship with a template library.

Thanks - Jay Daulton

-----Original Message-----
From: Irv Mullins [mailto:irv at ELLIJAY.COM]
Sent: Friday, November 12, 1999 1:59 PM
To: EUPHORIA at LISTSERV.MUOHIO.EDU
Subject: Re: String Search and Replace


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