About Duffbert...

Duffbert's Random Musings is a blog where I talk about whatever happens to be running through my head at any given moment... I'm Thomas Duff, and you can find out more about me here...

Email Me!

Search This Site!

Custom Search

I'm published!

Co-author of the book IBM Lotus Sametime 8 Essentials: A User's Guide
SametimeBookCoverImage.jpg

Purchase on Amazon

Co-author of the book IBM Sametime 8.5.2 Administration Guide
SametimeAdminBookCoverImage.jpg

Purchase on Amazon

MiscLinks

Visitor Count...



View My Stats

« NOW maybe we'll all believe Ed Brill and Alan Lepofsky... | Main| I'm now running the latest version of the BlogSphere template »

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

Comments

Gravatar Image1 - Hi, Marc... Usually what I do in those cases is try to copy the field into some sort of editor (like Notepad), and then review the file in a hex editor. I've found often that the special character is something like CHR(10), CHR(09), or CHR(13). In fact, I'll often try those without the hex editor just to see if they convert OK. If not, I'll go the hex editor route.

Gravatar Image2 - Blah, blah, blah...

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.

Gravatar Image3 - Hi Tom,
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

Gravatar Image4 - It wouldn't be an issue, except that I have been told that the Mid$ statement is much more efficient, and if you have to do it a bazillion times, I'd rather you had time to go and review another Java book instead of sitting around waiting for your LotusScript to finish.

Gravatar Image5 - Touche!

Gravatar Image6 - ... sigh...

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!


Gravatar Image7 - Julian,

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 This dented my professional pride.

We all make mistakes and I learnt from mine so I am just trying prevent any embrassment for anyone else.


Gravatar Image8 - I'll have to post screwed-up code more often!

That's what I do!

Gravatar Image9 - Fun, isn't it, Tom? Kind of like peer review of your code by total strangers.

Gravatar Image10 - Tom -

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

Gravatar Image11 - Ahah another ReplaceSubString function and another dodgy one at that.

scrubString("ANDAND ", "AND", "AND ") = Overflow error

Sorry could not resist

Gravatar Image12 - But HOW do you specify the 'bad stuff'???

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

Gravatar Image13 - Boy... for a somewhat small, offhand post, this sure generated the discussion. I'll have to post screwed-up code more often!

Gravatar Image14 - In the special case where strGoodStuff and strBadStuff are the same length, there is an even more efficient way, which uses the Mid$ statement (not function) to replace "in place". The code would then become:

'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)

Gravatar Image15 - little patch that allows it to replace & with & and not get in a fuss that & still exists

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

Post A Comment

:-D:-o:-p:-x:-(:-):-\:angry::cool::cry::emb::grin::huh::laugh::lips::rolleyes:;-)

Want to support this blog or just say thanks?

When you shop Amazon, start your shopping experience here.

When you do that, all your purchases during that session earn me an affiliate commission via the Amazon Affiliate program. You don't have to buy the book I linked you to (although I wouldn't complain!). Simply use that as your starting point.

Thanks!

Thomas "Duffbert" Duff

Ads of Relevance...