Split Command

Purpose

Splits a string into a Hash String (array of strings) by separating the string into substrings using regular expressions.

Syntax

Split hs[] = sexp, pattern [, max]

hs[]:Hash String
sexp, pattern:sexp
max:iexp

Description

Split returns the substrings of sexp in a Hash String, a list of strings. The Hash can be iterated over using For Each or a simple For Next. pattern is the string used to identify substring limits using a regular expression. The optional argument max specifies the maximum number of substrings to return. Although pattern can be a complex regular expression, it can also be a simple string that defines where the splits take place. For instance, the following example splits a string at a space character:

Dim hs As Hash String, s As String

Split hs[] = "This is a test", " "

For Each s In hs[]

Debug Each ` s

Next

Debug.Show

This prints in the Output window:

1 This
2 is
3 a
4 test

Now, if the string contained a double space before the word test, there would be five elements found of which the fourth is an empty string.

Dim hs As Hash String

Split hs[] = "This is a  test", " "

Debug hs[%]' prints 5

Debug.Show

Often, this is not wanted. In that case regular expressions solve the problem elegantly. The pattern argument can be changed to a group of spaces: "[ ]+". See reMatch for an overview of the patterns.

Split hs[] = "This is a  test", "[ ]+"

Example

Local h As Hash String, s As String

Split h[] = "name,surname;street,12,"#9",Cologne,,Fax: 0111-1234567 ", "\s*[,;]\s*"

For Each s In h[]

Debug Each`s

Next

Debug.Show

// this command splits the string into

h[% 1] = "name"
h[% 2] = "surname"
h[% 3] = "street"
h[% 4] = "12"
h[% 5] = ""
h[% 6] = "Cologne"
h[% 7] = ""
h[% 8] = "Fax: 0111-1234567"

The example shows a summary of personal information in a string separated by spaces, commas, tabs, and other white spaces. The search pattern is defined as: any number of spaces and/or tabs, then a comma or a semicolon, then again optional spaces or tabs. The string is separated with empty strings for missing details (commas, succeeding one another, with or without spaces or tabs between).

Remarks

The VB function ar$() = Split(sexp[, delimiter[, max[, compare]]]) is easily converted to GFA-BASIC 32. Rather than a string array, GFA-BASIC 32 uses a Hash String.

VB Split Function

GFA-BASIC 32 Split Command
Dim ar() As String
Dim sexp As String
Dim delim$ = " "
ar = Split(sexp, delim, ,0)
For i = 0 to UBound(ar())
Print ar(i)
Next
Dim hs As Hash String
Dim sexp As String
Dim delim$ = " "
Dim Cmp = Mode(Compare)
Mode Compare 0
Split hs[] = sexp, delim
Mode Compare Cmp
For i = 1 To hs[%]
Print hs[% i]
Next


The delimiter argument may contain only one character in VB. The compare mode indicates the kind of comparison to use when evaluating substrings. In GFA-BASIC 32 use the Mode Compare before executing the Split command. After executing the Mode Compare should be restored.

See Also

Join, reMatch, reSub, preMatch, Hash

{Created by Sjouke Hamstra; Last updated: 23/10/2014 by James Gaite}