OpenNTF.org - LS equivalent of @ReplaceSubst
My Links (Not logged in)
Code Bin Search
 
Hosted by Prominic.NET
Rate This Code
5 - brilliant stuff
4 - very nice
3 - average
2 - needs work
1 - bad
   OpenNTF Code Bin
About This Code
Brief Description:
LS equivalent of @ReplaceSubstring 
Rating:
Not Rated Yet 
Contributor:
Charles F. Phillips 
Category:
Lotusscript 
Type:
String functions 
Notes Version:
R4.x, R5.x 
Last Modified:
23 Dec 2002 
OpenNTF Disclaimer

All of the program code and information presented in the OpenNTF.org Code Bin are provided "as-is", and should be used at your own risk. OpenNTF.org make no express or implied warranty about anything in the Code Bin, and OpenNTF.org will not be responsible or liable for any damage caused by the use or misuse of anything from this site. OpenNTF.org makes no guarantees about anything. Please thoroughly test all of the knowledge and code you find here before you attempt to use them in your production environment.

Code / Description
Function searchReplace(startString As String, searchString As String, replaceString As String) As String


%REM
Function searchReplace
Charles F. Phillips
23 May 2001

This function does a simple text search and replace,
no matter how many occurances of the search string.
The beauty of this one is that it's backwards compatible
with R4 and you CAN include the search string as part
of the replacement string without worry of ending up in
an endless loop!
i.e., you can replace "&" with "&" and not go into a loop

%END REM

'just in case a non-string value was passed
On Error Resume Next

'dim strings used to reassemble final product
Dim sPart1 As String
Dim sPart2 As String
'dim iPos for use with Instr
Dim iPos As Integer

'dump incoming string into searchReplace in order to
'fulfill function's output even if searchstring isn't found
searchReplace = startString

'if startStrng or searchString were empty, then exit
If searchReplace ="" Or searchString ="" Then Exit Function

'Use Instr to determine if startString actually contains searchstring.
'We will care where it actually is located, but it's also a quick "Contains" type of check.
iPos = Instr(searchReplace, searchString)

'If Contains, then iPos will be a value higher than zero and the Do loop executes,
'but if not contains, then a zero is returned and we never enter Do loop.
Do Until iPos = 0

'Get everything to the left of the startstring
'iPos minus 1 will be all characters to the left of, but not including, searchstring
sPart1 = Left$(searchReplace, iPos-1)
'Get number of characters to the right of the searchstring:
'Total length of the string minus ((iPos-1) plus length of searchstring)
sPart2 = Right$(searchReplace, Len(searchReplace)-((iPos-1)+Len(searchString)))

'Reassemble searchReplace with parts 1 and 2 surrounding the replacement value
searchReplace = sPart1 & replaceString & sPart2

'Check to see if there is another occurance of the searchString,
'and if so, keep the loop going until no more occurances are found.
'Start looking after the last occurrance, plus the length of the replaceString
'in order to prevent something like search for "&" and replace with "&" from going into an infinite loop
iPos = Instr((iPos-1)+Len(replaceString), searchReplace, searchString)
Loop

End Function

Usage / Example
 Comments

No documents found

 Add your comment!