August 10, 2006

Lotusscript version of the @Middle formula

Here is a lotusscript function for the @Middle formula function.

Function Middle (fullString As String, startString As String, endString As String) As String

Dim startposition As Integer
Dim startlen As Integer
Dim endposition As Integer

startPosition = Instr (fullString, startString)
startLen = Len (startString)

If ((startPosition > 0) And (startString <> "")) Then
endPosition = Instr (Right$ (fullString, Len(fullString) - startPosition), endString)
If (endPosition > 0) Then
Middle = Mid$ (fullString, startPosition + startLen, Instr (startPosition + startLen, fullString, endString) - ( startPosition + startLen ) )
Else
Middle = Mid$ (fullString, Instr (fullString, startString) + Len (startString), Len (fullString))
End If
Else
Middle = ""
End If
End Function

Use:
midStr = Middle("This is a wildcard _ and this is what you get +", "_", "+")
Msgbox thestr

Result:
and this is what you get



*** updated 10 aug ***

or make it easier as Thomas Adrian suggested in a comment:

middle = Evaluate(|@Middle("| + mystring + |";"| + startString + |";"| + endstring + |")|)


Exempel:
Dim middle As Variant

middle = Evaluate(|@Middle("| + mystring + |";"| + startString + |";"| + endstring + |")|)
Print middle(0)


***************

*** updated 14 aug***

Here's another approach by Sean Burgess suggested in a comment and it works fine as well.

Dim tmp As Variant
Dim FullString As String
Dim StartString As String
Dim EndString As String

FullString = "This is a wildcard _ and this is what you get+"
StartString = "_"
EndString = "+"

tmp = Strright(Strleft(FullString, EndString), StartString)
Msgbox tmp


***************

One could say that the script I posted is somewhat unnecessary ;-) when it could be done in these ways. Or... you could call it ambitious. Anyway now I would say we've covered the middle function! But please feel free to post other solutions and I will put them up as updates.

Technorati tags:
, , ,

Andra bloggar om:
, , ,


2 comments:

Anonymous said...

Man kan göra hela ditt middlescript med en rad.
middle = Evaluate(|@Middle("| + mystring + |";"| + startString + |";"| + endstring + |")|)

Exempel:
Dim middle As Variant
middle = Evaluate(|@Middle("| + mystring + |";"| + startString + |";"| + endstring + |")|)
Print middle(0)


Se mer här:
http://www.notessidan.se/A55B53/blogg.nsf/plink/PPAN-6MEVDZ

Anonymous said...

Call me silly, but wouldn't a single line be much better?

tmp = StrRight(StrLeft(FullString, EndString), StartString)

Sean---