jcs:split() Function
Syntax
var $substrings = jcs:split(expression, string, <limit>)
Description
Split a string into an array of substrings delimited by a regular expression pattern. If the optional integer argument limit is specified, the function splits the entire string into limit number of substrings. If there are more than limit number of matches, the substrings include the first limit-1 matches as well as the remaining portion of the original string for the last match.
Parameters
- expression—Regular expression pattern used as the delimiter.
- limit—(Optional) Number of substrings into which to break the original string.
- string—Original string.
Return Value
- $substrings—Array of limit number of substrings
In the following example, the original string is "123:abc:456:xyz:789". The jcs:split() function breaks this string into substrings that are delimited by the regular expression pattern, which in this case is a colon(:). The optional parameter limit is not specified, so the function returns an array containing all the substrings that are bounded by the delimiter(:).
var $pattern = "(:)";
var $substrings = jcs:split($pattern, "123:abc:456:xyz:789");
returns:
$substrings[1] == "123"
$substrings[2] == "abc"
$substrings[3] == "456"
$substrings[4] == "xyz"
$substrings[5] == "789"
The following example uses the same original string and regular expression as the previous example, but in this case, the optional parameter limit is included. Specifying limit=2 causes the function to return an array containing only two substrings. The substrings include the first match, which is "123" (the same first match as in the previous example) and a second match, which is the remaining portion of the original string after the first occurrence of the delimiter.
var $pattern = "(:)";
var $substrings = jcs:split($pattern, "123:abc:456:xyz:789", 2);
returns:
$substrings[1] == "123"
$substrings[2] == "abc:456:xyz:789"
Hide Navigation Pane
Show Navigation Pane
Download
SHA1