logo

Sanity Check Needed

You know the LotusScript Replace() method? What does it do if you pass an empty string in to it as the "source array"? Bad things?

Imagine this code:

Call document.Replaceitemvalue( "Colours",
        Replace(
                document.GetItemValue("Colours"), 
                document.ReplaceThisColour(0), 
                document.ReplaceWithColour(0)
        )
)

The field called Colours is a multi-item field with the colours of the rainbow in it.

Now, if you enter "Blue" in to the "replace this" field and "Gold" in to the "replace with" field you get the following list returned, as expected:

Red, Orange, Yellow, Green, Gold, Indigo, Violet

But, what if you leave the "replace this" field empty? I'd have expected the list to remain unchanged. Nope. Every item in the lists gets replaced with an empty "" string.

Is this "expected behaviour" and just me being stupid or is it a well-known bug I never got to hear about?

The following code exhibits the same behaviour:

Dim colours(1 To 7) As String
        
colours(1) = "Red"
colours(2) = "Orange"
colours(3) = "Yellow"
colours(4) = "Green"
colours(5) = "Blue"
colours(6) = "Indigo"
colours(7) = "Violet"
        
Messagebox Implode(Replace(colours, "", "WTF?"), ", ")

The output is just six commas (", , , , , , "). Should this be?

Comments

  1. Perhaps the clue is in the documentation, where it says:

    Replace searches the String in sourceArray for the String in replaceArray. If a match is found, the substring is replaced with a corresponding substring from replacementArray. Each String in replaceArray is scanned against each String in sourceArray as modified by prior substitutions. Replace is case sensitive.

    Since it refers to "substring", perhaps the logic is that the substring which is the 0 characters starting with "" always matches the 0 characters starting with "" in the sourceArray, in which case the behavior would be correct. You could check to see if that is what it is doing by trying to using a substring such as "B" rather than "Blue". I assumed it worked with elements like @Replace, but it doesn't really say whether it does or not.

      • avatar
      • Jake Howlett
      • Mon 22 Mar 2010 01:34 PM

      That would explain it (I won't go as far as saying it would "make sense" though).

      there's a simple demo here:

      http://codestore.net/replace.nsf/test?openform

      If you put "e" in the "replace" field it puts "Gold" in all over the shop. Leave the replace field blank and it strips the whole list.

      It just doesn't make sense though. How can an empty string match every character in another string?

      Show the rest of this thread

    • avatar
    • PL
    • Mon 22 Mar 2010 02:05 PM

    I have seen lots of problems with the built in LS replace function, in the past have seen it crash agents if you give it too many large strings.

    I use a custom replace function using normal LS string handling, a good example is here

    http://www.breakingpar.com/bkp/home.nsf/0/A718016D2782D5CD87256AFB00131908

    • avatar
    • CJ
    • Tue 23 Mar 2010 04:24 AM

    If you're replacing whole values, could you use ArrayReplace instead?

      • avatar
      • Jake Howlett
      • Tue 23 Mar 2010 05:01 AM

      Good point CJ. That's probably what I'll do as the fix for this.

  2. For whatever reason (probably my early work with JavaScript) I have always used (since it became available) Join and Split instead of Replace.

    Mostly because I understand how Arrays are supposed to work in memory and it is a clean and predictable transition from a string split apart where the search string occurs into an array, and back to a string assembled with the replace string. It also behaves as you would expect Replace to do - that being not "finding" a match on an empty search string or source string.

    So, lovely Javascript: myString.split("e").Join("Gold");

    or LotusScript; myString = Join(Split( myString, "e"), "Gold")

    I know, it's more typing than just using Replace, but it's doing a specific sort of operation of which I am sure and trust in.

      • avatar
      • Jake Howlett
      • Tue 23 Mar 2010 02:47 PM

      Could this work for my case above where I want to replace items in an array and have it return an array?

      Show the rest of this thread

Your Comments

Name:
E-mail:
(optional)
Website:
(optional)
Comment:


About This Page

Written by Jake Howlett on Mon 22 Mar 2010

Share This Page

# ( ) '

Comments

The most recent comments added:

Skip to the comments or add your own.

You can subscribe to an individual RSS feed of comments on this entry.

Let's Get Social


About This Website

CodeStore is all about web development. Concentrating on Lotus Domino, ASP.NET, Flex, SharePoint and all things internet.

Your host is Jake Howlett who runs his own web development company called Rockall Design and is always on the lookout for new and interesting work to do.

You can find me on Twitter and on Linked In.

Read more about this site »

Elsewhere

Here are the external links posted on the same day.

More links are available in the archive »

More Content