Split Procedure

Version 7

 

 

Returns an array in a variant containing a specified number of substrings.

Syntax

[ vrStrArray = ] Split ( sInStr, [sDelimeter], [lCount], [bCompareNoCase] )

The Split procedure syntax has the following parts:

Name

Type

Description

vrStrArray

Variant

Return Value: an array in a variant containing a specified number of substrings.

sInStr

Val BString

A string containing substrings and delimiters.

sDelimeter

Val BString = " "

Optional. String character used to identify substring limits. If omitted, the space character (" ") is assumed to be the delimiter.

lCount

[Val] Long

Optional. Number of substrings to be returned; -1 (default) indicates that all substrings are returned.

bCompareNoCase

[Val] Bool

True (default) means no-case comparison, and False means case comparison.

Comments

If 'lCount' is 0, then it will return no string.  If 'lCount' is 1, then it return the original string.  If 'lCount' is more than the number of all substrings, then it returns all substrings.

If the input string is a zero-length string, Split returns an empty array, that is, a variant containing an array with no elements and no data.

If delimiter is a zero-length string, a single-element array containing the entire expression string is returned.

Calling Split with may return multiple empty entries elements if the delimiter is repeated in the input string. For example, calling with " " delimiter will return multiple empty string elements  if the sInStr has multiple consecutive spaces (i.e. "abc    def"). You can use Replace command to eliminate these consecutive spaces.

Example

In the following example Split returns an array from a string. It performs no-case comparison of the delimiter, and returns all of the substrings.

s = "ATEasyXisXEasy!"

vr = Split(s, "x")

iSize=VarDimSize(vr, 0)

if iSize>0

Redim as [iSize]

as=vr

for i=0 to iSize-1

print as[i]

next

endif

prints

 

ATEasy

is

Easy!

The following shows return values with different lCount, replace the Split statement above with the following statement:

 

Split(s, "x", 0)

returns a variant containing a  0 dimension string array - thus no string to be printed.

 

Split(s, "x", 1)   !prints the entire string: ""ATEasyXisXEasy!"

Split(s, "x", 2)

prints two substrings:

 

ATEasy

isXEasy!

 

Split(s, "x", 3)

Split(s, "x", 4)

Split(s, "x", 100)

all prints the three substrings as above when lCount is -1 (Default).

See Also

Pos, Mid, Right, Left, Filter, Join, Replace