< Back

Document Subject: ReplaceSubString for Lotusscript
Hint Short Cut: Add this to your code & documentation to help you find this page.
http://#Replacesubstring-lotusscript or http://A555F9/nn.nsf/ByAlias/Replacesubstring-lotusscript

Replacesubstring is not a lotusscript function. Here's my version.
Function ReplaceSubString(Byval phrase$, Byval oldStr$, Byval newStr$) As String

' This function searches the string passed in phrase for all
' occurences of the string passed in oldStr. It then replaces
' the oldStr with the specified newStr. If an error occurs,
' a NULL value is returned (""). 

Const FUNC_NAME= "ReplaceSubString2"
On Error Goto Errors
Dim begin%, found%, oldPhrase$

'---- Make sure that we have a phrase to change, and a string to look for:
If Strcompare(oldStr, "") = 0 Or Strcompare(phrase, "") = 0 Then Goto TheEnd
'---- Save the original string 
oldPhrase= phrase

begin = 1
found = Instr(begin, phrase, oldStr, 1) 
While (found > 0) 
 begin = found + Len(newStr)
'---- Changing an instance of oldString
 phrase = Left$(phrase, (found - 1)) & newStr & Right$(phrase, Len(phrase) - (found + Len(oldStr) - 1))
'--- Looking for a new instance of oldString
 found = Instr(begin, phrase, oldStr, 1)
Wend
'---- Returning the new phrase changed
ReplaceSubString = phrase

TheEnd:
Exit Function

Errors:
'---- Returning the original value of phrase
ReplaceSubString = oldPhrase
Resume TheEnd
End Function