Re: String Search and Replace

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

On Fri, 12 Nov 1999, Jay Daulton wrote:

> I have been going over the Euphoria doc. and am impressed with the speed,
> and data structure simplicity.
> Can someone post a string search and replace function? One of the reasons I
> am looking around for a better language to program in is because the ones I
> use, APL and J (both interpreted) are too slow for some of things I want to
> do - especially string search and replace. I think that Euphoria will
> benchmark quite a bit better than what I am currently using in J.
>
> Thanks - Jay Daulton

Here is a simple example I just whipped up:

-- tested code starts here --
sequence string1, string2

function replace( sequence string, sequence findstring, sequence
replacestring )
  integer loc, len

  len   = length( findstring )
  loc = match( findstring, string )

  if loc then
    return string[1..(loc - 1) ] &
           replacestring &
           string[ (loc + len)..length( string ) ]
  else
    return string
  end if

end function


string1 = "Euphoria is fast, flexible and fun; simple, safe, and sexy!\n"
string2 = replace( string1, "sexy", "super sexy" )
puts( 1, string1 )
puts( 1, string2 )
-- end tested code --

Sorry about lack of comments but it should be pretty straight-forward.  If
the sequence is not found, the function simply returns the original,
unmodified string.  You can modify it to return whatever you want...

Hope this helps,
Brian

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

Search



Quick Links

User menu

Not signed in.

Misc Menu