Friday, September 9, 2011

VBScript Split

VBScript Split
VBScript Split function returns a zero-based, one-dimensional array containing a specified number of substrings.

Syntax of VBScript Split
Split(expression[, delimiter[, count[, compare]]])

where compare is 0 for vbBinaryCompare and 1 for vbTextCompare.

Example 1 of VBScript Split
Dim Str, MyArray, i
Str = "Learning|is|fun!"
MyArray = Split(Str, "|", -1, 1)
'MyArray(0) contains "Learning".
'MyArray(1) contains "is".
'MyArray(2) contains "fun!".

For i = 0 to UBound(MyArray)
MsgBox (MyArray(i)) 'shows msgbox 3 times, first time with Learning, second time with is and third time with fun.
Next

In above example,Str is an expression that contains strings, | is string character used to identify substring limits, default is the "space character", -1 is number of substrings to be returned and -1 here indicates that all substrings are returned and lastly 1 Specifies the string comparison to use: 0 = vbBinaryCompare - Perform a binary comparison and 1 = vbTextCompare - Perform a textual comparison

Example 2 of VBScript Split
Dim Str, MyArray, i
Str = "Learningdancingswimming!" 'Learning dancing swimming! is written without spaces.
MyArray = Split(Str, "g", -1, 1)

For i = 0 to UBound(MyArray)
MsgBox (MyArray(i)) 'shows msgbox 4 times, first time with Learnin, second time with dancin and third time with swimmin and fourth time with !.
Next

Example 3 of VBScript Split
Dim Str, MyArray, i
Str = "Learningdancingswimming!" 'Learning dancing swimming! is written without spaces.
MyArray = Split(Str, "G", -1, 0)

For i = 0 to UBound(MyArray)
MsgBox (MyArray(i)) 'shows Learningdancingswimming!
Next

Example 4 of VBScript Split
Dim Str, MyArray, i
Str = "Learningdancingswimming!" 'Learning dancing swimming! is written without spaces.
MyArray = Split(Str, "g", -1, 0)

For i = 0 to UBound(MyArray)
MsgBox (MyArray(i)) 'shows msgbox 4 times, first time with Learnin, second time with dancin and third time with swimmin and fourth time with !.
Next

Example 5 of VBScript Split
Dim Str, MyArray, i
Str = "Learningdancingswimming!" 'Learning dancing swimming! is written without spaces.
MyArray = Split(Str, "g", 2, 0)

For i = 0 to UBound(MyArray)
MsgBox (MyArray(i)) 'shows msgbox two times, firstly Learnin and secondly dancingswimming!
Next


Also See:
Complete List of VBScript Array Functions