Core Function Split

From Sputnik Wiki
Revision as of 12:01, 10 November 2011 by UberFoX (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Split( <expression>, <delim/pattern>, <flag> )

Contents

Description

Splits up a string into substrings depending on the given delimiters.

OR

Splits up a string into substrings depending on the given RegExp pattern.

Parameters

expression

The string to evaluate.

delim/pattern

One or more characters to use as delimiters (case sensitive).

OR

A RegExp match pattern.

flag

Warning: You can only use the flag if you are NOT using a RegExp pattern as the Delim.

Optional; Changes how the string split works: flag = 0 (default), each character in the delimiter string will mark where to split the string. flag = 1, entire delimiter string is needed to mark where to split the string.

Return Value

Returns an array with each elment ($array[0], $array[1], etc.) contain the delimited strings.

If no delimiters were found an empty array will be returned.

Remarks

If you use a blank string "" for the delimiters, each character will be returned as an element.

If you use a blank RegExp m// for the delim, each character will be returned as an element.

If the delimiter you wish to use is a substring instead of individual single characters, see the example below.

Split is very useful as an alternative to InStr and as a means to populate an array.

Caution if you use the macro @CRLF you are referring to a 2 character string so you will generate extra blanks lines.

Example

To split a string at each , example:

$days = Split("Sun,Mon,Tue,Wed,Thu,Fri,Sat", ",")
For $day In $days
	println("Element is: " . $day)
Next

To split a string at each letter example:

$letters = Split("The quick brown FoX", "")
For $letter In $letters
	println("Element is: " . $letter)
Next

To split a string every time it finds the word "moo" example:

$array = Split("This moo is a string moo ok", "moo", 1)
For $elem In $array
	println("Element is: " . $elem)
Next

You can place the Split directly in the For loop example:

For $elem In Split("This moo is a string moo ok", "moo", 1)
	println("Element is: " . $elem)
Next

To split the string each time a "<" followed by digits followed by a ">" is fonud using a RegExp pattern example:

$str = Split("The<12>quick<46>brown<68>fox....", m/<\d+>/)
For $elem In $str
	println("Element is: " . $elem)
Next

Of course you can place the split in the For example:

For $elem In Split("The<12>quick<46>brown<68>fox....", m/<\d+>/)
	println("Element is: " . $elem)
Next

To see what more patterns you can match go see Regex Match or RegExp functions and read the Remarks.

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox