String Replace Function For Lotus Script

Here is a handy function that I sometimes use in my Lotus Script programs. 

'===============================================================================
' +----------------------------------------------------------------------------+
' | Function: strreplace
' +----------------------------------------------------------------------------+
' | Accepts: src, the value to search for to replace.
' |          dest, the value to replace with.
' |          arg, the string to search within.
' +----------------------------------------------------------------------------+
' | Description:
' | Replaces all occurances of src with dest in the string arg.
' +----------------------------------------------------------------------------+
'===============================================================================
Public Function strreplace(Byval src As String, Byval dest As String,
Byval arg As String) As String

    'Declare variables
    Dim pos As Integer

    'Initialize
    pos = Instr(arg, src)

    'Loop through the string
    While (pos > 0)
        arg = Left(arg, pos - 1) + dest + Mid(arg, pos + Len(src))
        pos = Instr(pos + Len(dest), arg, src)
    Wend

    'Return the replaced string
    strreplace = arg

End Function

Here is an example of how to use the function:

newString = strreplace("WORLD", "World", "Hello WORLD!")

The newString variable would contain “Hello World!”

You may also like...

5 Responses

  1. Frederic Plourde says:

    Thanks for this nice piece of code. It helped me a lot with a Domino 5 server.

  2. Kenny says:

    Great! I was saved by this script. Thanks!

  3. Charles says:

    Nice Great one

  4. Alex says:

    There is a standard Replace function in LotusScript for quite a while.

  5. @Alex,
    Thanks for the info. It’s been quite a while since I have written anything in LotusScript.

Leave a Reply

Your email address will not be published. Required fields are marked *