LotusScript function to do character replacement in a string variable...
Category Software Development
In some of my coding projects, I have a need to, within a LotusScript agent, scrub certain characters out of a string variable, like line feeds. I use the following function to take care of that for me. I'm posting it here mainly so that I can easily find it when I need it next time (and I don't have a copy of an app that uses it), but if you find it of use, feel free to "R&D" it (Rob & Duplicate)...
Function scrubString(strField As String, strBadStuff As String, strGoodStuff As String) As String
'This function will search the passed string for the "bad" stuff to be removed, and will replace it with the "good" stuff
Dim intLength As Integer
Dim intReplacePos As Integer
'Keep ripping and replacing while "bad" stuff still exists
Do While Instr(strField, strBadStuff) > 0
intLength = Len(strField)
intReplacePos = Instr(strField, strBadStuff)
strField = Left(strField, intReplacePos -1) + strGoodStuff + Right(strField, intLength - intReplacePos)
Loop
'Return the cleaned up field to the calling routine
scrubString = strField
End Function
In some of my coding projects, I have a need to, within a LotusScript agent, scrub certain characters out of a string variable, like line feeds. I use the following function to take care of that for me. I'm posting it here mainly so that I can easily find it when I need it next time (and I don't have a copy of an app that uses it), but if you find it of use, feel free to "R&D" it (Rob & Duplicate)...
Function scrubString(strField As String, strBadStuff As String, strGoodStuff As String) As String
'This function will search the passed string for the "bad" stuff to be removed, and will replace it with the "good" stuff
Dim intLength As Integer
Dim intReplacePos As Integer
'Keep ripping and replacing while "bad" stuff still exists
Do While Instr(strField, strBadStuff) > 0
intLength = Len(strField)
intReplacePos = Instr(strField, strBadStuff)
strField = Left(strField, intReplacePos -1) + strGoodStuff + Right(strField, intLength - intReplacePos)
Loop
'Return the cleaned up field to the calling routine
scrubString = strField
End Function



Comments
Posted by Duffbert At 15:53:48 On 30/05/2007 | - Website - |
Seriously, that's a good point. In many of my uses, the "special" case of matching string lengths is the normal case. I'd like to say I decided to go my direction to handle both scenarios, but I'd most likely be lying...
Thanks for the feedback, and I'll have to see what I can do with the routine.
Posted by Thomas Duff At 07:12:10 On 03/12/2003 | - Website - |
3 years after you posted your original comment, Google directed me to this page.
my company is still running R5, not R6 with its fancy FindandReplace method.
The net result is: I tweaked your function so it works with different strings of different lengths.
The full function is now:
Function scrubString(strField As String, strBadStuff As String, strGoodStuff As String) As String
'This function will search the passed string for the "bad" stuff to be removed, and will replace it with the "good" stuff
Dim intLength As Integer
Dim badLength As Integer
Dim intReplacePos As Integer
'Keep ripping and replacing while "bad" stuff still exists
'note that the search is case insensitive
Do While Instr(strField, strBadStuff) > 0
intLength = Len(strField)
badLength = Len(strBadStuff)
intReplacePos = Instr(strField, strBadStuff)
strField = Left(strField, IntReplacePos -1) + strGoodStuff + Right(strField, intLength - intReplacePos - badLength + 1)
Loop
'Return the cleaned up field to the calling routine
scrubString = strField
End Function
Posted by Marcus Powrie At 03:59:11 On 18/04/2006 | - Website - |
Posted by Ben Langhinrichs At 07:23:11 On 03/12/2003 | - Website - |
Posted by Thomas Duff At 07:48:08 On 03/12/2003 | - Website - |
Here I try to help, and what do I get? Nuthin' but grief!
I'll just go back to book reviews, where I KNOW what I'm doing!
Posted by Thomas Duff At 11:54:31 On 03/12/2003 | - Website - |
You are correct I am obsessed with ReplaceSubString routines. I spotted another dodgy ReplaceSubString in the OpenNTF codebin last month.
My fixation is mainly due to an embarassing incident where I added a ReplaceSubString routine to a WebQueryOpen agent. Then the function went into an inifinite loop and the http task needed to be restart on a live server
We all make mistakes and I learnt from mine so I am just trying prevent any embrassment for anyone else.
Posted by John Marshall At 02:05:53 On 04/12/2003 | - Website - |
That's what I do!
Posted by Ben Poole At 08:39:57 On 04/12/2003 | - Website - |
Posted by Ben Langhinrichs At 11:30:37 On 03/12/2003 | - Website - |
Don't feel bad, I went through some growing pains when I was working out some ReplaceSubstring code in Java not too long ago (too lazy to look for the link on my site right now). It seemed like such an easy thing at first, and then I started testing some unusual situations...
In fact, now that I think about it, John Marshall chimed in on that one too. I think he might be obsessed with ReplaceSubstring.
Seriously, though. It's better to find out about those things now than when you're trying to debug a 1000 line production agent.
- Julian
Posted by Julian Robichaux At 15:16:00 On 03/12/2003 | - Website - |
scrubString("ANDAND ", "AND", "AND ") = Overflow error
Sorry could not resist
Posted by John Marshall At 10:11:36 On 03/12/2003 | - Website - |
In debugger I am seeing an upright rectangle that is likely a hard return or paragraph mark. How do I specify either of those in a script agent?
Thanks,
Marc Robson
Posted by Marc Robson At 14:20:53 On 30/05/2007 | - Website - |
Posted by Thomas Duff At 04:28:26 On 04/12/2003 | - Website - |
'Keep ripping and replacing while "bad" stuff still exists
Do While Instr(strField, strBadStuff) > 0
Mid$(strField, Instr(strField, strBadStuff)) = strGoodStuff
Loop
'Return the cleaned up field to the calling routine
scrubString = strField
I hope that you can incorporate it into an even more efficient function in the future. (Of course, with this you can also pass the original in by reference to a subroutine and never set the scrubString = strField at the end, if the original string is to be revised)
Posted by Ben Langhinrichs At 06:41:14 On 03/12/2003 | - Website - |
Do While Instr(intReplacePos,strField, strBadStuff) > 0
intLength = Len(strField)
badLength = Len(strBadStuff)
intReplacePos = Instr(intReplacePos,strField, strBadStuff)
strField = Left(strField, IntReplacePos -1) + strGoodStuff + Right(strField, intLength - intReplacePos - badLength + 1)
intReplacePos=intReplacePos+1
Loop
Posted by Alan Bell At 07:51:47 On 28/11/2008 | - Website - |