ReplaceSubstring in LotusScript

Full code for the function is inserted below as well as an example.

Please note that the code may wrap…

Sub Initialize
   'declarations
   Dim s As New NotesSession
   Dim dc As NotesDocumentCollection
   Dim doc As NotesDocument
   Dim result As Variant

   Dim look_for(1) As String
   look_for(0) = TEST_OLD_URL_RESOURCE
   look_for(1) = TEST_OLD_URL_SCANNET

   Dim replace_with(1) As String
   replace_with(0) = TEST_NEW_URL_RESOURCE
   replace_with(1) = TEST_NEW_URL_SCANNET

   Dim process_fields(5) As String
   process_fields(0) = "txtLinksDisplayData_1"
   process_fields(1) = "txtLinksDisplayData_2"
   process_fields(2) = "txtLinksTargetData_1"
   process_fields(3) = "txtLinksTargetData_2"
   process_fields(4) = "txtLinks_1"
   process_fields(5) = "txtLinks_2"

   Set dc = s.CurrentDatabase.UnprocessedDocuments
   Set doc = dc.GetFirstDocument
   While Not (doc Is Nothing)
      'loop fields and replace
      Forall field In process_fields
         result = ReplaceSubstring(doc.GetItemValue(field), look_for, replace_with)
         Call doc.ReplaceItemValue(field, result)
      End Forall

      'save
      Call doc.Save(True, False)

      'get next
      Set doc = dc.GetNextDocument(doc)
   Wend
End Sub
Public Function ReplaceSubstring(look_in As Variant, look_for As Variant, replace_with As Variant) As Variant
   'validate parameters
   If Isarray(look_for) And Isarray(replace_with) Then
      'both are arrays - make sure they are the same size
      If Ubound(look_for)  Ubound(replace_with) Then
         'different size
         Error 9999, "The look_for and replace_with arrays must be of the same size."
      End If
   Elseif (Isarray(look_for) Xor Isarray(replace_with))  Then
      'either look_for is an array and replace_with isn't or nice versa
      Error 9999, "If look_for is an array so must replace_with or vice versa."
   End If

   If Isarray(look_in) Then
      'handle all entries in the array
      Dim result_array() As Variant
      Dim result As Variant
      Redim result_array(Ubound(look_in))
      Dim count As Integer

      'loop
      Forall v In look_in
         'make sure we do not run on objects
         If Not Isobject(v) Then
            'do replace
            result = pReplaceSubstring(v, look_for, replace_with)

            'store result in array
            result_array(count) = result
         Else
            'keep original value
            Set result_array(count) = v
         End If

         'increment count
         count = count + 1
      End Forall

      'return result
      ReplaceSubstring = result_array
   Else
      'handle single value - make sure we do not run on objects
      If Not Isobject(look_in) Then
         'do replace
         ReplaceSubstring = pReplaceSubstring(look_in, look_for, replace_with)
      Else
         'return original value
         Set ReplaceSubstring = look_in
      End If
   End If
End Function

Private Function pReplaceSubstring(Byval look_in As Variant, look_for As Variant, replace_with As Variant) As Variant
   'make sure the input is a string
   If Typename(look_in)  "STRING" Then
      'return original value
      If Isobject(look_in) Then
         Set pReplaceSubstring = look_in
      Else
         pReplaceSubstring = look_in
      End If
      Exit Function
   End If

   'is there an array of things to look for ?
   If Isarray(look_for) Then
      'yes - handle all values
      Dim i As Integer

      'loop - arrays garanteed to be the same size be calling function
      For i=0 To Ubound(look_for)
         'do replacesubstring
         look_in = pReplaceSingleValue(look_in,look_for(i), replace_with(i))
      Next

      'return
      pReplaceSubstring = look_in
   Else
      'nope - single value
      pReplaceSubstring = pReplaceSingleValue(look_in,look_for, replace_with)
   End If

End Function

Private Function pReplaceSingleValue(look_in As Variant, look_for As Variant, replace_with As Variant) As String
   'declarations
   Dim index_start As Integer
   Dim index_stop As Integer
   Dim first_part As String
   Dim last_part As String
   Dim result As String

   'look for the value
   index_start = Instr(1, look_in, look_for)
   If index_start > 0 Then
      'we found the value - get the part before what we are
      'looking for (if not at the beginning)
      If index_start > 1 Then
         first_part = Mid$(look_in, 1, index_start-1)
      End If

      'calcuate the stop index
      index_stop = index_start + Len(look_for)

      'get the last part (if not past the end of the string)
      If index_stop < Len(look_in) Then
         last_part = Mid$(look_in, index_stop)
      End If

      'concatenate the parts
      result = first_part + replace_with + last_part

      'return
      pReplaceSingleValue = result
   Else
      'substring not found - return the original value
      pReplaceSingleValue = look_in
   End If
End Function

Published by

lekkim

Positive, competent, out-spoken, frank and customer focused architect and developer with a strong foundation in web, cloud and product development. I'm a strong advocate for API first and cloud based solutions and development. I have a knack for being able to communicate and present technically complicated matters in conference, customer and training settings. I've previously acted as team member and leader in a product organisation.