Skip to content

Latest commit

 

History

History
87 lines (61 loc) · 1.62 KB

File metadata and controls

87 lines (61 loc) · 1.62 KB
layout api-command
language Java
permalink api/java/split/
command split
related_commands
upcase downcase match
upcase/
downcase/
match/

Command syntax

{% apibody %} string.split([separator, [max_splits]]) → array {% endapibody %}

Description

Split a string into substrings. With no arguments, will split on whitespace; when called with a string as the first argument, will split using that string as a separator. A maximum number of splits can also be specified. (To specify max_splits while still splitting on whitespace, use null as the separator argument.)

Mimics the behavior of Python's string.split in edge cases, except for splitting on the empty string, which instead produces an array of single-character strings.

Example: Split on whitespace.

r.expr("foo  bar bax").split().run(conn);

Result:

["foo", "bar", "bax"]

Example: Split the entries in a CSV file.

r.expr("12,37,,22,").split(",").run(conn);

Result:

["12", "37", "", "22", ""]

Example: Split a string into characters.

r.expr("mlucy").split("").run(conn);

Result:

["m", "l", "u", "c", "y"]

Example: Split the entries in a CSV file, but only at most 3 times.

r.expr("12,37,,22,").split(",", 3).run(conn);

Result:

["12", "37", "", "22,"]

Example: Split on whitespace at most once (i.e. get the first word).

r.expr("foo  bar bax").split(null, 1).run(conn);

Result:

["foo", "bar bax"]