Core Function Split

From Sputnik Wiki
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 (The pattern is very similar to perl and layed out the same way).

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.

flag = 2, the delimiter will be treated as an array and each string in it can 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", ",");
Foreach( $days as $day )
{
	println("Element is: " . $day);
}

To split a string at each letter example:

$letters = Split("The quick brown FoX", "");
Foreach ( $letters as  $letter )
{
	println("Element is: " . $letter);
}

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

$array = Split("This moo is a string moo ok", "moo", 1);
Foreach ( $array as $elem )
{
	println("Element is: " . $elem);
}

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

Foreach ( Split("This moo is a string moo ok", "moo", 1) as $elem )
{
	println("Element is: " . $elem);
}

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

$str = Split("The<12>quick<46>brown<68>fox....", m/<\d+>/);
Foreach ( $str as $elem )
{
	println("Element is: " . $elem);
}

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

Foreach ( Split("The<12>quick<46>brown<68>fox....", m/<\d+>/) as $elem )
{
	println("Element is: " . $elem);
}

Now split it using an array of strings to use as the delimiters

$str = Split("The1111quick2222brown2222fox1111jumps2222omg", array("1111", "2222"), 2);
Foreach ( $str as $elem )
{
	println("Element is: " . $elem);
}

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