split
string
Splits string by separator.
Installation
Import
import { split } from '@tulx/utils';Source Code
Implementation
/**
* Splits string by separator.
*
* @param string - The string to split.
* @param separator - The separator pattern to split by.
* @param limit - The length to truncate results to.
* @returns Returns the string segments.
*
* @example
* ```ts
* split('a-b-c', '-', 2); // ['a', 'b']
* ```
*/
export function split(
string: string,
separator?: string | RegExp,
limit?: number
): string[] {
if (limit === undefined) {
return string.split(separator as string | RegExp);
}
return string.split(separator as string | RegExp, limit);
}
Example
import { split } from '@tulx/utils';
split('a-b-c', '-', 2); // ['a', 'b']Related Functions
camelCase
Converts string to camel case.
capitalize
Converts the first character of string to upper case and the remaining to lower case.
deburr
Deburrs string by converting Latin-1 Supplement and Latin Extended-A letters to basic Latin letters and removing combining diacritical marks.
endsWith
Checks if string ends with the given target string.
escape
Converts the characters "&", "<", ">", '"', and "'" in string to their corresponding HTML entities.
escapeRegExp
Escapes the RegExp special characters "^", "$", "\", ".", "*", "+", "?", "(", ")", "[", "]", "{", "}", and "|" in string.