One line @ReplaceSubString for lotusscript

[Update: Read the comments for a better solution with short strings (unverified)!]

Had this problem removing single characters in a string. I don’t like the idea to use strRight() and strLeft() since they can only handle a single entry of the search string.

There are a lot of different solution for this. I have also made my own lotusscript function for @replaceSubString, as every one else. All to complex since Release 6.

Some split and join will do the trick. This is my one liner:
resultStr = join( split( sourceStr, fromStr ), toStr )

The replace is case sensitive.

Example replacing word:
ReplaceSubString Example1

Button code:

Dim ui As New NotesUIWorkspace
Dim uidoc As NotesUIDocument
Dim doc As NotesDocument
Set uidoc = ui.CurrentDocument
Set doc = uidoc.Document
doc.result = Join(Split(doc.text(0),doc.Replace(0)),doc.replacewith(0))

Example removing character:
ReplaceSubString Example2

Or make a function out of it to make it more readable:

Function replaceSubString(sourceStr As String, fromStr As String, toString As String) As String

replaceSubString = Join(Split(sourceStr,fromStr),toString)

End Function

Advertisements

17 Responses to “One line @ReplaceSubString for lotusscript”

  1. Julian Robichaux Says:

    I might be overlooking something, but why don’t you just use the “Replace” function? Like:

    result = Replace(doc.text(0),doc.Replace(0),doc.replacewith(0))

    The documentation makes it look like you can only use arrays, but Strings work just as well.

  2. Jan Van Puyvelde Says:

    And what was the problem you had with the standard Replace function ?

  3. Vitor Pereira Says:

    Nice and easy, the way it should be! Thanks for that.

  4. Tommy Valand Says:

    Why not just use Evaluate?

    From your example:
    doc.result = Join( Evaluate( |@ReplaceSubstring(text; replace; replacewith)|, doc ) )

    Or with strings
    someString = Join( Evaluate( |@ReplaceSubstring(“Hello World!” ; “Hello” ; “Goodbye cruel”)| ) )

  5. Tomas Ekström Says:

    @Julian and @Jan You are absolutely right! Still living in R5 even though join/split is 6. I know I have had a look on replace, but discarded it because my first try was not successful. I only got it to work as @replace and not @replacesubstring.
    I wish I had the code to see what I did get wrong.

    @Tommy I’ve learned something new to day! Hope you did I to 🙂

  6. Tommy Valand Says:

    I thought there was a replace (I sometimes forget what language does what), but since you posted a Replace function, I guessed I was thinking about another language.. 🙂

    (In a pathetic attempt to hide my shame)
    The Evaluate way also should work pre R6 😛

  7. Julian Robichaux Says:

    Tomas: Well, part of the problem is the documentation. The help file really does NOT make it clear that you can use anything other than Arrays with the Replace function.

    Otherwise, you’re probably just like me. I always find a way to do something and then keep on doing it that way. I don’t think I started using StrLeft and StrRight on a regular basis until R7. And I still definitely live in the R5 era when I use @Functions. I just can’t help myself.
    😉

  8. quintessens Says:

    @Tommy:

    and what do I do if I want to do:

    someString = Join( Evaluate( |@ReplaceSubstring(“doc\position” ; “\” ; “\\”)| ) )

    the last “\\” gives an error in the Evaluate macro

    • Ashish Gawande Says:

      ‘Check this out for replacing “\” with “\\”
      Dim ret As Variant
      Dim teststr As String
      Dim delim As String
      ‘Example path
      filepath=c:\Documents and Settings\\Desktop\BRD.doc
      ret = Split(filepath, “\”)
      WstrPath=””
      Forall i In ret
      WstrPath=WstrPath+i+”\\”
      End Forall
      ‘***Result will be -c:\\Documents and Settings\\Desktop\\BRD.doc

  9. Tomas Ekström Says:

    @Quintessens
    You are just forgetting it is a formula string.
    The fault is not in “\\” but in the preceding “\”.
    \” is translated to the string value of “.
    A syntax that will evaluate correctly is:
    someString = Join( Evaluate( |@ReplaceSubstring(”doc\position” ; “\\” ; “\\”)| ) )

    But that makes no sense since you are replacing to the same string.
    I believe you wish to do this:
    someString = Join( Evaluate( |@ReplaceSubstring(”doc\position” ; “\\” ; “\\\\”)| ) )

    That would replace a single \ with a double \ (\\).

    Still this particular string would not give any result since there is no backslash in the string to replace in. “doc\position” would evaluate to “docposition”. That string has to be written like this “doc\\position”.

    But if you are using LotusScript. I would suggest doing it like this:
    Replace(“doc\position”,”\”,”\\”)

    Save’s you a lot of time if you like to use a string variable instead of “doc\position” Since you will have an egg or hen problem (who came first). When using the first solution, you have to replace single backslashes in the variable with double ones, to get it to work.

  10. Vamsi Says:

    User has new unread emails in his inbox and he states that as soon as he reads them it gets disappeared from inbox.After reading all the emails his inbox in empty.What is the issue what will you do to fix this?

    • KAM Says:

      He’s probably looking at the inbox with the “view only un-read emails” box checked. Not that this post is over an year old or anything….

      • Tomas Ekström Says:

        Actually the notifications have not been working. Still no excuse for not attending to my blog. The last year I had a lot, but things looks better now. Hopefully the notifications will work from now on to.

  11. larry wolf Says:

    Note- Replace function does not appear to handle strings past a certain size- only the first half of a 965 character string is searched/replaced when I try it in V7. Join/Split approach works just right; I am using that. Thank you!

  12. Miljbee Says:

    Something to remember :

    Sub Initialize
    Dim s As String
    Dim s2 As String
    Dim startTime As Variant
    Dim dspRes As String
    Dim runTime As variant

    Dim count As Integer
    Dim length As Long

    length=2
    s=”ab”
    While length<65000
    Print "Testing a " + Len(s) + " characters long string with …"
    startTime = Now
    For count=1 To 1000 Step 1
    s2 = Replace(s,"a","A",1,Len(s),0)
    Next
    runTime = Now-startTime
    dspRes = Left$(s2,2) +" (…) " +Right$(s2,2)
    Print "Result="+dspRes+", took " + CStr(cint((runTime)*3600*24))+"s with replace"

    startTime = Now
    For count=1 To 1000 Step 1
    s2 = Join(Split(s,"a"),"A")
    Next
    runTime = Now-startTime
    dspRes = Left$(s2,2) +" (…) " +Right$(s2,2)
    Print "Result="+dspRes+", took " + CStr(CInt((runTime)*3600*24))+"s with split/join"

    If length<4096 Then
    startTime = Now
    For count=1 To 1000 Step 1
    s2 = Join(Evaluate({@ReplaceSubstring("}+s+{";"a";"A")}))
    Next
    runTime = Now-startTime
    dspRes = Left$(s2,2) +" (…) " +Right$(s2,2)
    Print "Result="+dspRes+", took " + CStr(CInt((runTime)*3600*24))+"s with Join(evaluate())"
    End If

    length=length*2
    s=s & s
    Wend
    End Sub

    Output :
    Testing a 2 characters long string with …
    Result=Ab (…) Ab, took 0s with replace
    Result=Ab (…) Ab, took 0s with split/join
    Result=Ab (…) Ab, took 0s with Join(evaluate())
    Testing a 4 characters long string with …
    Result=Ab (…) Ab, took 0s with replace
    Result=Ab (…) Ab, took 0s with split/join
    Result=Ab (…) Ab, took 0s with Join(evaluate())
    Testing a 8 characters long string with …
    Result=Ab (…) Ab, took 0s with replace
    Result=Ab (…) Ab, took 0s with split/join
    Result=Ab (…) Ab, took 1s with Join(evaluate())
    Testing a 16 characters long string with …
    Result=Ab (…) Ab, took 0s with replace
    Result=Ab (…) Ab, took 0s with split/join
    Result=Ab (…) Ab, took 0s with Join(evaluate())
    Testing a 32 characters long string with …
    Result=Ab (…) Ab, took 0s with replace
    Result=Ab (…) Ab, took 0s with split/join
    Result=Ab (…) Ab, took 0s with Join(evaluate())
    Testing a 64 characters long string with …
    Result=Ab (…) Ab, took 0s with replace
    Result=Ab (…) Ab, took 1s with split/join
    Result=Ab (…) Ab, took 0s with Join(evaluate())
    Testing a 128 characters long string with …
    Result=Ab (…) Ab, took 0s with replace
    Result=Ab (…) Ab, took 1s with split/join
    Result=Ab (…) Ab, took 0s with Join(evaluate())
    Testing a 256 characters long string with …
    Result=Ab (…) Ab, took 1s with replace
    Result=Ab (…) Ab, took 1s with split/join
    Result=Ab (…) Ab, took 0s with Join(evaluate())
    Testing a 512 characters long string with …
    Result=Ab (…) Ab, took 2s with replace
    Result=Ab (…) Ab, took 2s with split/join
    Result=Ab (…) Ab, took 0s with Join(evaluate())
    Testing a 1024 characters long string with …
    Result=Ab (…) Ab, took 3s with replace
    Result=Ab (…) Ab, took 6s with split/join
    Result=Ab (…) Ab, took 0s with Join(evaluate())
    Testing a 2048 characters long string with …
    Result=Ab (…) Ab, took 5s with replace
    Result=Ab (…) Ab, took 11s with split/join
    Result=Ab (…) Ab, took 1s with Join(evaluate())
    Testing a 4096 characters long string with …
    Result=Ab (…) Ab, took 11s with replace
    Result=Ab (…) Ab, took 23s with split/join
    Testing a 8192 characters long string with …
    Result=Ab (…) Ab, took 23s with replace
    Result=Ab (…) Ab, took 45s with split/join
    Testing a 16384 characters long string with …
    Result=Ab (…) Ab, took 44s with replace
    Result=Ab (…) Ab, took 89s with split/join
    Testing a 32768 characters long string with …
    Result=Ab (…) Ab, took 88s with replace
    Result=Ab (…) Ab, took 181s with split/join

    The evaluate don't pass the 4096 Test.

    So if your string has less than 2048 characters, you should use evaluate with @replacesubstring, else, use ls replace.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: