Tip

Explode Function

You Can View User Feedback To This Tip

Similar to Lotus' @Explode function, this Explode LotusScript function behaves as follows:
--Given a text string, elements that are separated by the specified separator are defined as members of an array.

For example if the string that was passed was "a:bc:def:g", and the separator was defined as ":", then the resulting array would contain the following four values:
a
bc
def
g


Function explode (Byval inString As String, OutList() As String, delim As String)

'This function takes a text field and splits the values into members of an array, based on the value passed as delim

Dim idx As Integer
Dim begin As Integer
Dim i As Integer

i = 0
begin = 1


'First make sure that the last character in inString is not a delimiter If Right$ ( inString , Len(delim) ) = delim Then inString = Left$(inString,Len(inString) - Len(delim))
End If

idx = Instr (begin , inString , delim)
Do While idx > 0
Redim Preserve outList(0 To i) As String
outList(i) = Mid$ ( inString , begin , idx - begin )
i = i + 1
begin = idx + Len(delim)
idx = Instr (begin , inString , delim)
Loop

Redim Preserve outList(0 To i) As String
outList(i) = Mid$ ( inString , begin )
explode = i + 1
End Function

USER FEEDBACK TO THIS TIP

  • Another way to implement the explode function would be using evaluate. In fact, use evaluate whenever you can make a computation with the formula language. It is so much more efficient.
     Dim texttosplit As String Dim resultarray As Variant Const delim = ":" texttosplit = "a:bc:def:g" resultarray = Evaluate(|@explode("| & texttosplit & |";"| & delim & |")|)
    
    Remember to avoid redim statements when you can. They're expensive.—Kasper
  • The code for the script version of explode is a bit different from the @Function. The @Function uses every character in the string as a delimiter. For example, if I have a string "No1Yes2Maybe3" and I use @Explode(string; "123"), I will get No, Yes, Maybe. But if I use your script, I don't get any list array back, just my original string because my delimiter is not in the string as a string. But for a one-character delimiter, it works the same. >—Robert Albritton

This was first published in November 2000

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.