startsWith
string
Checks if string starts with the given target string.
Installation
Import
import { startsWith } from '@tulx/utils';Source Code
Implementation
/**
* Checks if string starts with the given target string.
*
* @param string - The string to inspect.
* @param target - The string to search for.
* @param position - The position to search from.
* @returns Returns true if string starts with target, else false.
*
* @example
* ```ts
* startsWith('abc', 'a'); // true
* startsWith('abc', 'b'); // false
* startsWith('abc', 'b', 1); // true
* ```
*/
export function startsWith(
string: string,
target: string,
position: number = 0
): boolean {
const { length } = string;
const pos = position < 0 ? 0 : position > length ? length : position;
return string.substring(pos, pos + target.length) === target;
}
Example
import { startsWith } from '@tulx/utils';
startsWith('abc', 'a'); // true
startsWith('abc', 'b'); // false
startsWith('abc', 'b', 1); // trueRelated 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.