Skip to content

String

Functions to manipulate strings immutably and type-safely. All functions preserve the original string and return a new value.

How to import?

The library exposes the DString and S namespaces from the main entry or via direct import (tree-shaking friendly), which lets you only load what you need.

typescript
import { DString, S } from "@duplojs/utils";
import * as DString from "@duplojs/utils/string";
import * as S from "@duplojs/utils/string";

Transformation

capitalize

Uppercases the first letter of a string.

uncapitalize

Lowercases the first letter of a string.

toLowerCase

Converts the entire string to lowercase.

toUpperCase

Converts the entire string to uppercase.

normalize

Normalizes a Unicode string to a specific form.

repeat

Repeats a string a specified number of times.

replace

Replaces the first occurrence of a pattern in a string.

replaceAll

Replaces all occurrences of a pattern in a string.

Search and test

includes

Checks whether a string contains a substring.

startsWith

Checks whether a string starts with a specific substring.

endsWith

Checks whether a string ends with a specific substring.

indexOf

Returns the index of the first occurrence of a substring.

lastIndexOf

Returns the index of the last occurrence of a substring.

Searches for a match with a regular expression.

match

Retrieves matches of a regular expression.

matchAll

Retrieves all matches of a regular expression with their groups.

Extraction

charAt

Returns the character at a specific index.

at

Returns the character at an index (supports negative indexes).

slice

Extracts a section of a string.

substring

Returns a substring between two indexes.

split

Splits a string into an array of substrings.

Padding and trim

padStart

Pads the start of a string to a given length.

padEnd

Pads the end of a string to a given length.

trim

Removes whitespace from the start and end of a string.

trimStart

Removes whitespace from the start of a string.

trimEnd

Removes whitespace from the end of a string.

Utilities

concat

Concatenates multiple strings together.

sort

Sorts an array of strings in ascending or descending order.

sortCompare

Compares two strings using locale-aware sorting rules.

isKeyof

Checks whether a string is a valid key of an object (type guard).

Released under the MIT license.