@ReplaceSubstring Function in Lotus Script – Made Easy


Hello everyone,

Hope you all doing good with your life. For one of my recent prject, i was looking for a @replacesubstring equivalent function in Lotus Script and started writing my own customized function finally. While i was busy with the functions i thought about a new idea, which i think is a quick way to achieve the same @replacesubstring functionality with Lotus Script.

For example, i need to replace “B” to “2” from the following string “ABCDEFGHIJKLMANOP”

This was the function that i was busy writing.

Function ReplaceSubstring (fullstr As String, oldstr As String, newstr As String) As String

dim lenold as string
dim pos as integer
lenold = Len (oldstr)
pos = Instr (fullstr, oldstr)
Do While pos > 0 And oldstr <> “”
fullstr = Left (fullstr, pos – 1) & newstr & Mid (fullstr, pos + lenold)
pos = Instr (pos + Len (newstr), fullstr, oldstr)
Loop
ReplaceSubstring = fullstr
End Function

Function ReplaceSubstring (fullstr As String, oldstr As String, newstr As String) As String

dim lenold as string

dim pos as integer
lenold = Len (oldstr)

pos = Instr (fullstr, oldstr)

Do While pos > 0 And oldstr <> “”

fullstr = Left (fullstr, pos – 1) & newstr & Mid (fullstr, pos + lenold)

pos = Instr (pos + Len (newstr), fullstr, oldstr)

Loop

ReplaceSubstring = fullstr

End Function

While i had completed writing this function, i thought about the other easy and quick approach to do the same functionality.

Sub Click(Source As Button)

Dim toReplace As Variant

toReplace   = Join(Split(“ABCDEFGHIJKLMNOP”,”B”),”2″)

msgbox toReplace

End Sub

How cool is that, it gives me the same result as the above function 🙂

If you find this useful then please use it in your projects.

Till next time, cheers.

Advertisements

3 thoughts on “@ReplaceSubstring Function in Lotus Script – Made Easy

  1. Kamal,

    Why do you need to write Formula function completely in LS if Evaluate function do it for you.. Same @ReplaceSubString function can be done like,

    Dim w As String,macro As String
    Dim eval As Variant

    w= “ABCDEFGHIJKLMANOP”

    macro={@ReplaceSubString(“}+w+{“;”B”;”2″}+{)}

    eval=Evaluate(macro)

    Msgbox eval(0)

    Rishi

  2. Also, It’s not a good idea to split single word with specific alphabets which you have tried
    in your second solution. BTW, I don’t think so second solution is working ?

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