A string is a sequence of characters. In JavaScript, strings are not mutable. Any transfomation of a string, such as slice
or concat
generates a new string. The JavaScript runtime however may optimize this behavior by mutating strings behind the scenes, when it can be guarenteed that the previous string is not accessible to the programmer. The runtime may also avoid copying slices of a string, or even concatenation of slices of strings, by implementing it as a series of lookups into existing strings. Food for thought.
This package provides functions for generating spaces, querying about a string, comparing strings, getting parts of a string, searching a string, transforming a string or its case, finding ngrams in strings, finding similarity/distance between strings, and array-like functions. All built-in string functions are also included. Finally, constants for ASCII characters, and minimum/maximum code point are included.
import * as xstring from "jsr:@nodef/extra-string";
xstring.longestCommonInfix('dismiss', 'mississipi');
// → 'miss'
xstring.longestUncommonInfixes('chocolatier', 'engineer');
// → ['chocolati', 'engine']
xstring.toKebabCase('Malwa Plateau');
// → 'malwa-plateau'
'6.626 x 10' + xstring.toSuperscript('-34');
// → '6.626 x 10⁻³⁴' (Planck's constant)
xstring.tverskyDistance('pikachu', 'raichu', 3, 0.2, 0.4);
// → 0.6666666666666667
Name | Description |
---|---|
toKebabCase | Convert a string to kebab-case. |
toSnakeCase | Convert a string to snake-case. |
toCamelCase | Convert a string to camel-case. |
toPascalCase | Convert a string to pascal-case. |
toSlugCase | Convert a string to slug-case (URL-friendly kebab-case). |
toWords | Split a string into words, after de-casing it. |
toBaseline | Convert a string to baseline characters (limited support). |
toSuperscript | Convert a string to superscript characters (limited support). |
toSubscript | Convert a string to superscript characters (limited support). |
ngrams | Get n-grams of a string. |
uniqueNgrams | Find unique n-grams of a string. |
countNgrams | Count the total number of n-grams of a string. |
countUniqueNgrams | Count the total number of unique n-grams of a string. |
countEachNgram | Count each n-gram of a string. |
matchingNgrams | Get matching n-grams between strings. |
uniqueMatchingNgrams | Get unique matching n-grams between strings. |
countMatchingNgrams | Count the total number of matching n-grams between strings. |
countEachMatchingNgram | Count each matching n-gram between strings. |
countUniqueMatchingNgrams | Count the total number of unique matching n-grams between strings. |
euclideanDistance | Get euclidean distance between strings. |
hammingDistance | Get hamming distance between strings. |
jaccardIndex | Get jaccard index between strings. |
jaccardDistance | Get jaccard distance between strings. |
sorensenDiceIndex | Get Sørensen-Dice index between strings. |
sorensenDiceDistance | Get Sørensen-Dice distance between strings. |
tverskyIndex | Get Tversky index between strings. |
tverskyDistance | Get Tversky distance between strings. |
jaroSimilarity | Get Jaro similarity between strings. |
jaroDistance | Get Jaro distance between strings. |
jaroWinklerSimilarity | Get Jaro-Winkler similarity between strings. |
jaroWinklerDistance | Get Jaro-Winkler distance between strings. |
levenshteinDistance | Get Levenshtein distance between strings. |
damerauLevenshteinDistance | Get Damerau–Levenshtein distance between strings. |
longestCommonInfix | Get the longest common infix between strings. |
longestCommonPrefix | Get the longest common prefix of strings. |
longestCommonSuffix | Get the longest common suffix of strings. |
longestUncommonInfixes | Get the longest uncommon infixes of strings. |
get | Get character at a given index in string. |
getAll | Get characters at indices. |
set | Write a substring at specified index in string. |
begin | Get leftmost part of string. |
middle | Get a portion of string from middle. |
end | Get rightmost part of string. |
fromCharCode | Get characters whose UTF-16 code units are given. |
fromCodePoint | Get characters whose unicode code points are given. |
concat | Combine multiple strings into one. |
repeat | Repeat string given number of times. |
valueOf | Get primitive value of string object. |
length | Get length of string. |
charAt | Get character at given index in string. |
charCodeAt | Get UTF-16 code unit of a character in string. |
codePointAt | Get unicode code point of a character in string. |
localeCompare | Compare two strings in the current or given locale. |
includes | Check if string has a given infix. |
startsWith | Check if string has a given prefix. |
endsWith | Check if string has a given suffix. |
indexOf | Get first index of a given infix in string. |
lastIndexOf | Get last index of a given infix in string. |
search | Get first index of regular expression match in string. |
match | Get results of matching string with regular expression. |
matchAll | Get detailed results of matching string with regular expression. |
toString | Get string representation of string. |
slice | Extract section of string. |
substring | Extract section of string. |
split | Split string by a given separator into substrings. |
trimStart | Remove whitespace from begining of string. |
trimEnd | Remove whitespace from end of string. |
trim | Remove whitespace from begining and end of string. |
padStart | Pad start of string to fit a desired length. |
padEnd | Pad end of string to fit a desired length. |
toUpperCase | Convert string to upper case. |
toLocaleUpperCase | Convert string to upper case, as per locale-specific case mappings. |
toLowerCase | Convert string to lower case. |
toLocaleLowerCase | Convert string to lower case, as per locale-specific case mappings. |
replace | Replace first match of given pattern by replacement. |
normalize | Normalize string by given form, as per Unicode Standard Annex #15. |
of | Create string from arguments, like Array.of() . |
from | Create string from iterable, like Array.from() . |
splice | Remove/replace characters in a string. |
reverse | Reverse a string. |
sort | Arrange characters in an order. |
filter | Filter characters which pass a test. |
spaces | Get a string of spaces. |
is | Check if value is a string. |
isEmpty | Check if string is empty. |
isCharacter | Check if string is a character. |
index | Get non-negative index within string. |
indexRange | Get non-negative index range within string. |
codePointRange | Get unicode code point range of string. |
compare | Compare two strings. |
isEqual | Check if two strings are equal. |
DIGITS | Decimal digits 0-9. |
OCT_DIGITS | Octal digits 0-7. |
HEX_DIGITS | Hexadecimal digits 0-9, A-F, a-f. |
UPPERCASE | English letters A-Z. |
LOWERCASE | English letters a-z. |
LETTERS | Combination of uppercase, lowercase english letters. |
PUNCTUATION | Punctuation symbols (ASCII). |
WHITESPACE | The string "\t\n\x0b\x0c\r ". |
PRINTABLE | Combination of digits, letters, punctuation, and whitespace (ASCII). |
MIN_CODE_POINT | Minimum unicode code point. |
MAX_CODE_POINT | Maximum unicode code point. |
- MDN Web docs
- Lodash documentation
- natural package by Chris Umbel and contributors
- esrever package by Mathias Bynens and contributors
- js-string-compression package by Chen Caishun
- @stdlib/string-base-trim package by @stdlib-js
- How do you reverse a string in-place in JavaScript?
As of 26 June 2025, this project is licensed under AGPL-3.0. Previous versions remain under MIT.