splitString()function
splitString(str: string, separator: string, min: 1, max: 1, caller?: AnyCaller): readonly [string]
splitString(str: string, separator: string, min: 2, max: 2, caller?: AnyCaller): readonly [string, string]
splitString(str: string, separator: string, min: 3, max: 3, caller?: AnyCaller): readonly [string, string, string]
splitString(str: string, separator: string, min: 4, max: 4, caller?: AnyCaller): readonly [string, string, string, string]
splitString(str: string, separator: string, min?: 1, max?: number, caller?: AnyCaller): readonly [string, ...string[]]
splitString(str: string, separator: string, min: 2, max?: number, caller?: AnyCaller): readonly [string, string, ...string[]]
splitString(str: string, separator: string, min: 3, max?: number, caller?: AnyCaller): readonly [string, string, string, ...string[]]
splitString(str: string, separator: string, min: 4, max?: number, caller?: AnyCaller): readonly [string, string, string, string, ...string[]]
splitString(str: string, separator: string, min?: number, max?: number, caller?: AnyCaller): ImmutableArray<string>
| Param | Type | |
|---|---|---|
str | string | The string to divide. required |
separator | string | The separator to divide str on. required |
min | 1 | The minimum number of segments required (defaults to 1). required |
max | 1 | The maximum number of segments (excess segments are concatenated onto the last; defaults to Infinity). required |
caller | AnyCaller | Function to attribute a thrown error to (defaults to splitString itself). |
str | string | required |
separator | string | required |
min | 2 | required |
max | 2 | required |
caller | AnyCaller | Any calling function or constructor, usually referring to something that can call in the current scope that can appear in a stack trace. |
min | 3 | required |
max | 3 | required |
min | 4 | required |
max | 4 | required |
min | 1 | |
max | number | |
min | number |
| Return | |
|---|---|
readonly [string] | Array of the divided segments. |
readonly [string, string] | |
readonly [string, string, string] | |
readonly [string, string, string, string] | |
readonly [string, ...string[]] | |
readonly [string, string, ...string[]] | |
readonly [string, string, string, ...string[]] | |
readonly [string, string, string, string, ...string[]] | |
ImmutableArray<string> | Immutable array: an array that cannot be changed. |
| Throws | |
|---|---|
ValueError | If min isn't met, or if any of the segments are empty. |
Divide a string into parts based on a separator.
- Like
String.prototype.split()but with more useful arguments. - Excess segments in
String.prototype.split()is counterintuitive because further parts are thrown away. - Excess segments in
splitString()are concatenated onto the last segment (setmaxtonullif you want infinite segments).
Examples
splitString("a-b-c", "-", 2, 2) // ["a", "b-c"]