This npm package provides several utility functions for working with strings in TypeScript. Below are the details of each function along with code examples for usage.
capitalizeConverts the first character of a string to uppercase.
str: The input string.The string with the first character in uppercase.
import { capitalize } from 'stringUtils';
const str = 'hello';
const capitalizedStr = capitalize(str);
console.log(capitalizedStr); // Output: 'Hello'
decapitalizeConverts the first character of a string to lowercase.
str: The input string.The string with the first character in lowercase.
import { decapitalize } from 'stringUtils';
const str = 'Hello';
const decapitalizedStr = decapitalize(str);
console.log(decapitalizedStr); // Output: 'hello'
toLowerCaseConverts a string to lowercase.
str: The input string.The lowercase version of the string.
import { toLowerCase } from 'stringUtils';
const str = 'HELLO';
const lowerCaseStr = toLowerCase(str);
console.log(lowerCaseStr); // Output: 'hello'
toUpperCaseConverts a string to uppercase.
str: The input string.The uppercase version of the string.
import { toUpperCase } from 'stringUtils';
const str = 'hello';
const upperCaseStr = toUpperCase(str);
console.log(upperCaseStr); // Output: 'HELLO'
trimRemoves leading and trailing whitespace from a string.
str: The input string.The string with leading and trailing whitespace removed.
import { trim } from 'stringUtils';
const str = ' hello ';
const trimmedStr = trim(str);
console.log(trimmedStr); // Output: 'hello'
startsWithChecks if a string starts with the specified prefix.
str: The input string.prefix: The prefix to check for.True if the string starts with the prefix, false otherwise.
import { startsWith } from 'stringUtils';
const str = 'hello';
const prefix = 'he';
const startsWithPrefix = startsWith(str, prefix);
console.log(startsWithPrefix); // Output: true
endsWithChecks if a string ends with the specified suffix.
str: The input string.suffix: The suffix to check for.True if the string ends with the suffix, false otherwise.
import { endsWith } from 'stringUtils';
const str = 'hello';
const suffix = 'lo';
const endsWithSuffix = endsWith(str, suffix);
console.log(endsWithSuffix); // Output: true
reverseStrReverses a string.
str: The input string.The reversed string.
import { reverseStr } from 'stringUtils';
const str = 'hello';
const reversedStr = reverseStr(str);
console.log(reversedStr); // Output: 'olleh'
countOccurrencesCounts the occurrences of a substring within a string.
str: The input string.subStr: The substring to search for.The number of occurrences of the substring in the string.
import { countOccurrences } from 'stringUtils';
const str = 'hello world hello';
const subStr = 'hello';
const occurrences = countOccurrences(str, subStr);
console.log(occurrences); // Output: 2