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>
ParamType
strstring
The string to divide. required
separatorstring
The separator to divide str on. required
min1
The minimum number of segments required (defaults to 1). required
max1
The maximum number of segments (excess segments are concatenated onto the last; defaults to Infinity). required
callerAnyCaller
Function to attribute a thrown error to (defaults to splitString itself).
strstring
required
separatorstring
required
min2
required
max2
required
callerAnyCaller
Any calling function or constructor, usually referring to something that can call in the current scope that can appear in a stack trace.
min3
required
max3
required
min4
required
max4
required
min1
maxnumber
minnumber
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 (set max to null if you want infinite segments).

Examples

splitString("a-b-c", "-", 2, 2) // ["a", "b-c"]