Base (i.e., lower-level) string functions.
npm install @stdlib/string-baseWe believe in a future in which the web is a preferred environment for numerical computation. To help realize this future, we've built stdlib. stdlib is a standard library, with an emphasis on numerical and scientific computation, written in JavaScript (and C) for execution in browsers and in Node.js. The library is fully decomposable, being architected in such a way that you can swap out and mix and match APIs and functionality to cater to your exact preferences and use cases. When you use stdlib, you can be absolutely certain that you are using the most thorough, rigorous, well-written, studied, documented, tested, measured, and high-quality code out there. To join us in bringing numerical computing to the web, get started by checking us out on GitHub, and please consider financially supporting stdlib. We greatly appreciate your continued support!
About stdlib...
[![NPM version][npm-image]][npm-url] [![Build Status][test-image]][test-url] [![Coverage Status][coverage-image]][coverage-url]
> Base (i.e., lower-level) string functions.
``bash`
npm install @stdlib/string-base
`javascript`
var string = require( '@stdlib/string-base' );
#### string
Namespace containing "base" (i.e., lower-level) string functions.
`javascript`
var ns = string;
// returns {...}
The namespace contains the following functions:
- [altcase( str )][@stdlib/string/base/altcase]: convert a string to alternate case.
- [atob( str )][@stdlib/string/base/atob]: decode a string of data which has been encoded using Base64 encoding.
- [base64ToUint8Array( str )][@stdlib/string/base/base64-to-uint8array]: convert a Base64-encoded string to a Uint8Array.
- [camelcase( str )][@stdlib/string/base/camelcase]: convert a string to camel case.
- [capitalize( str )][@stdlib/string/base/capitalize]: capitalize the first character in a string.
- [codePointAt( string, position, backward )][@stdlib/string/base/code-point-at]: return a Unicode code point from a string at a specified position.
- [constantcase( str )][@stdlib/string/base/constantcase]: convert a string to constant case.
- [distances][@stdlib/string/base/distances]: implementations of various string similarity metrics.
- [dotcase( str )][@stdlib/string/base/dotcase]: convert a string to dot case.
- [endsWith( str, search, len )][@stdlib/string/base/ends-with]: test if a string ends with the characters of another string.
- [firstCodePoint( str, n )][@stdlib/string/base/first-code-point]: return the first n Unicode code points of a string.
- [firstGraphemeCluster( str, n )][@stdlib/string/base/first-grapheme-cluster]: return the first n grapheme clusters (i.e., user-perceived characters) of a string.
- [first( str, n )][@stdlib/string/base/first]: return the first n UTF-16 code units of a string.
- [forEachCodePointRight( str, clbk[, thisArg ] )][@stdlib/string/base/for-each-code-point-right]: invokes a function for each Unicode code point in a string, iterating from right to left.
- [forEachCodePoint( str, clbk[, thisArg ] )][@stdlib/string/base/for-each-code-point]: invokes a function for each Unicode code point in a string.
- [forEachGraphemeCluster( str, clbk[, thisArg ] )][@stdlib/string/base/for-each-grapheme-cluster]: invokes a function for each grapheme cluster (i.e., user-perceived character) in a string.
- [forEachRight( str, clbk[, thisArg ] )][@stdlib/string/base/for-each-right]: invokes a function for each UTF-16 code unit in a string, iterating from right to left.
- [forEach( str, clbk[, thisArg ] )][@stdlib/string/base/for-each]: invokes a function for each UTF-16 code unit in a string.
- [formatInterpolate( tokens, ...args )][@stdlib/string/base/format-interpolate]: generate string from a token array by interpolating values.
- [formatTokenize( str )][@stdlib/string/base/format-tokenize]: tokenize a string into an array of string parts and format identifier objects.
- [headercase( str )][@stdlib/string/base/headercase]: convert a string to HTTP header case.
- [invcase( str )][@stdlib/string/base/invcase]: convert a string to inverse case.
- [kebabcase( str )][@stdlib/string/base/kebabcase]: convert a string to kebab case.
- [lastCodePoint( str, n )][@stdlib/string/base/last-code-point]: return the last n Unicode code points of a string.
- [lastGraphemeCluster( str, n )][@stdlib/string/base/last-grapheme-cluster]: return the last n grapheme clusters (i.e., user-perceived characters) of a string.
- [last( str, n )][@stdlib/string/base/last]: return the last n UTF-16 code units of a string.
- [lpad( str, len, pad )][@stdlib/string/base/left-pad]: left pad a string.
- [ltrim( str )][@stdlib/string/base/left-trim]: trim whitespace characters from the beginning of a string.
- [lowercase( str )][@stdlib/string/base/lowercase]: convert a string to lowercase.
- [pascalcase( str )][@stdlib/string/base/pascalcase]: convert a string to Pascal case.
- [percentEncode( str )][@stdlib/string/base/percent-encode]: percent-encode a UTF-16 encoded string according to RFC 3986.
- [removeFirstCodePoint( str, n )][@stdlib/string/base/remove-first-code-point]: remove the first n Unicode code points of a string.
- [removeFirstGraphemeCluster( str, n )][@stdlib/string/base/remove-first-grapheme-cluster]: remove the first n grapheme clusters (i.e., user-perceived characters) of a string.
- [removeFirst( str, n )][@stdlib/string/base/remove-first]: remove the first n UTF-16 code units of a string.
- [removeLastCodePoint( str, n )][@stdlib/string/base/remove-last-code-point]: remove the last n Unicode code points of a string.
- [removeLastGraphemeCluster( str, n )][@stdlib/string/base/remove-last-grapheme-cluster]: remove the last n grapheme clusters (i.e., user-perceived characters) of a string.
- [removeLast( str, n )][@stdlib/string/base/remove-last]: remove the last n UTF-16 code units of a string.
- [repeat( str, n )][@stdlib/string/base/repeat]: repeat a string a specified number of times and return the concatenated result.
- [replaceAfterLast( str, search, replacement, fromIndex )][@stdlib/string/base/replace-after-last]: replace the substring after the last occurrence of a specified search string.
- [replaceAfter( str, search, replacement, fromIndex )][@stdlib/string/base/replace-after]: replace the substring after the first occurrence of a specified search string.
- [replaceBeforeLast( str, search, replacement, fromIndex )][@stdlib/string/base/replace-before-last]: replace the substring before the last occurrence of a specified search string.
- [replaceBefore( str, search, replacement, fromIndex )][@stdlib/string/base/replace-before]: replace the substring before the first occurrence of a specified search string.
- [replace( str, search, newval )][@stdlib/string/base/replace]: replace search occurrences with a replacement string.
- [reverseCodePoints( str )][@stdlib/string/base/reverse-code-points]: reverse the Unicode code points of a string.
- [reverseGraphemeClusters( str )][@stdlib/string/base/reverse-grapheme-clusters]: reverse the grapheme clusters (i.e., user-perceived characters) of a string.
- [reverse( str )][@stdlib/string/base/reverse]: reverse the UTF-16 code units of a string.
- [rpad( str, len, pad )][@stdlib/string/base/right-pad]: right pad a string.
- [rtrim( str )][@stdlib/string/base/right-trim]: trim whitespace characters from the end of a string.
- [sliceCodePoints( str, start, end )][@stdlib/string/base/slice-code-points]: slice a string based on Unicode code point indices.
- [sliceGraphemeClusters( str, start, end )][@stdlib/string/base/slice-grapheme-clusters]: slice a string based on grapheme cluster (i.e., user-perceived character) indices.
- [slice( str, start, end )][@stdlib/string/base/slice]: slice UTF-16 code units from a string.
- [snakecase( str )][@stdlib/string/base/snakecase]: convert a string to snake case.
- [startcase( str )][@stdlib/string/base/startcase]: capitalize the first letter of each word in a string.
- [startsWith( str, search, position )][@stdlib/string/base/starts-with]: test if a string starts with the characters of another string.
- [stickycase( str[, p] )][@stdlib/string/base/stickycase]: convert a string to sticky case.
- [trim( str )][@stdlib/string/base/trim]: trim whitespace characters from the beginning and end of a string.
- [truncateMiddle( str, len, seq )][@stdlib/string/base/truncate-middle]: truncate the middle UTF-16 code units of a string to return a string having a specified length.
- [uncapitalize( str )][@stdlib/string/base/uncapitalize]: uncapitalize the first character of a string.
- [uppercase( str )][@stdlib/string/base/uppercase]: convert a string to uppercase.
`javascript
var ns = require( '@stdlib/string-base' );
// Generate a Pascal case string...
var str = ns.pascalcase( 'beep boop' );
// returns 'BeepBoop'
// Tokenize a string into an array of string parts and format identifier objects...
str = 'The %d %s foxes jumped over the %d %s dogs.';
var tokens = ns.formatTokenize( str );
// returns [ 'The ', {...}, ' ', {...}, ' foxes jumped over the ', {...}, ' ', {...}, ' dogs.' ]
// Generate a string from a token array by interpolating values...
str = ns.formatInterpolate( tokens, 3, 'quick', 4, 'lazy' );
// returns 'The 3 quick foxes jumped over the 4 lazy dogs.'
// Test whether a string starts with the characters of another string...
str = 'Lorem ipsum dolor sit amet';
var bool = ns.startsWith( str, 'Lorem' );
// returns true
// Test whether a string ends with the characters of another string...
bool = ns.endsWith( str, 'amet' );
// returns true
// Trim whitespace characters from the beginning and end of a string...
str = ' \t\n Lorem ipsum dolor sit amet \n\t ';
str = ns.trim( str );
// returns 'Lorem ipsum dolor sit amet'
`
*
This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.
For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib].
#### Community
[![Chat][chat-image]][chat-url]
---
See [LICENSE][stdlib-license].
Copyright © 2016-2026. The Stdlib [Authors][stdlib-authors].
[npm-image]: http://img.shields.io/npm/v/@stdlib/string-base.svg
[npm-url]: https://npmjs.org/package/@stdlib/string-base
[test-image]: https://github.com/stdlib-js/string-base/actions/workflows/test.yml/badge.svg?branch=v0.3.1
[test-url]: https://github.com/stdlib-js/string-base/actions/workflows/test.yml?query=branch:v0.3.1
[coverage-image]: https://img.shields.io/codecov/c/github/stdlib-js/string-base/main.svg
[coverage-url]: https://codecov.io/github/stdlib-js/string-base?branch=main
[chat-image]: https://img.shields.io/badge/zulip-join_chat-brightgreen.svg
[chat-url]: https://stdlib.zulipchat.com
[stdlib]: https://github.com/stdlib-js/stdlib
[stdlib-authors]: https://github.com/stdlib-js/stdlib/graphs/contributors
[umd]: https://github.com/umdjs/umd
[es-module]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules
[deno-url]: https://github.com/stdlib-js/string-base/tree/deno
[deno-readme]: https://github.com/stdlib-js/string-base/blob/deno/README.md
[umd-url]: https://github.com/stdlib-js/string-base/tree/umd
[umd-readme]: https://github.com/stdlib-js/string-base/blob/umd/README.md
[esm-url]: https://github.com/stdlib-js/string-base/tree/esm
[esm-readme]: https://github.com/stdlib-js/string-base/blob/esm/README.md
[branches-url]: https://github.com/stdlib-js/string-base/blob/main/branches.md
[stdlib-license]: https://raw.githubusercontent.com/stdlib-js/string-base/main/LICENSE
[@stdlib/string/base/altcase]: https://www.npmjs.com/package/@stdlib/string-base-altcase
[@stdlib/string/base/atob]: https://www.npmjs.com/package/@stdlib/string-base-atob
[@stdlib/string/base/base64-to-uint8array]: https://www.npmjs.com/package/@stdlib/string-base-base64-to-uint8array
[@stdlib/string/base/camelcase]: https://www.npmjs.com/package/@stdlib/string-base-camelcase
[@stdlib/string/base/capitalize]: https://www.npmjs.com/package/@stdlib/string-base-capitalize
[@stdlib/string/base/code-point-at]: https://www.npmjs.com/package/@stdlib/string-base-code-point-at
[@stdlib/string/base/constantcase]: https://www.npmjs.com/package/@stdlib/string-base-constantcase
[@stdlib/string/base/distances]: https://www.npmjs.com/package/@stdlib/string-base-distances
[@stdlib/string/base/dotcase]: https://www.npmjs.com/package/@stdlib/string-base-dotcase
[@stdlib/string/base/ends-with]: https://www.npmjs.com/package/@stdlib/string-base-ends-with
[@stdlib/string/base/first-code-point]: https://www.npmjs.com/package/@stdlib/string-base-first-code-point
[@stdlib/string/base/first-grapheme-cluster]: https://www.npmjs.com/package/@stdlib/string-base-first-grapheme-cluster
[@stdlib/string/base/first]: https://www.npmjs.com/package/@stdlib/string-base-first
[@stdlib/string/base/for-each-code-point-right]: https://www.npmjs.com/package/@stdlib/string-base-for-each-code-point-right
[@stdlib/string/base/for-each-code-point]: https://www.npmjs.com/package/@stdlib/string-base-for-each-code-point
[@stdlib/string/base/for-each-grapheme-cluster]: https://www.npmjs.com/package/@stdlib/string-base-for-each-grapheme-cluster
[@stdlib/string/base/for-each-right]: https://www.npmjs.com/package/@stdlib/string-base-for-each-right
[@stdlib/string/base/for-each]: https://www.npmjs.com/package/@stdlib/string-base-for-each
[@stdlib/string/base/format-interpolate]: https://www.npmjs.com/package/@stdlib/string-base-format-interpolate
[@stdlib/string/base/format-tokenize]: https://www.npmjs.com/package/@stdlib/string-base-format-tokenize
[@stdlib/string/base/headercase]: https://www.npmjs.com/package/@stdlib/string-base-headercase
[@stdlib/string/base/invcase]: https://www.npmjs.com/package/@stdlib/string-base-invcase
[@stdlib/string/base/kebabcase]: https://www.npmjs.com/package/@stdlib/string-base-kebabcase
[@stdlib/string/base/last-code-point]: https://www.npmjs.com/package/@stdlib/string-base-last-code-point
[@stdlib/string/base/last-grapheme-cluster]: https://www.npmjs.com/package/@stdlib/string-base-last-grapheme-cluster
[@stdlib/string/base/last]: https://www.npmjs.com/package/@stdlib/string-base-last
[@stdlib/string/base/left-pad]: https://www.npmjs.com/package/@stdlib/string-base-left-pad
[@stdlib/string/base/left-trim]: https://www.npmjs.com/package/@stdlib/string-base-left-trim
[@stdlib/string/base/lowercase]: https://www.npmjs.com/package/@stdlib/string-base-lowercase
[@stdlib/string/base/pascalcase]: https://www.npmjs.com/package/@stdlib/string-base-pascalcase
[@stdlib/string/base/percent-encode]: https://www.npmjs.com/package/@stdlib/string-base-percent-encode
[@stdlib/string/base/remove-first-code-point]: https://www.npmjs.com/package/@stdlib/string-base-remove-first-code-point
[@stdlib/string/base/remove-first-grapheme-cluster]: https://www.npmjs.com/package/@stdlib/string-base-remove-first-grapheme-cluster
[@stdlib/string/base/remove-first]: https://www.npmjs.com/package/@stdlib/string-base-remove-first
[@stdlib/string/base/remove-last-code-point]: https://www.npmjs.com/package/@stdlib/string-base-remove-last-code-point
[@stdlib/string/base/remove-last-grapheme-cluster]: https://www.npmjs.com/package/@stdlib/string-base-remove-last-grapheme-cluster
[@stdlib/string/base/remove-last]: https://www.npmjs.com/package/@stdlib/string-base-remove-last
[@stdlib/string/base/repeat]: https://www.npmjs.com/package/@stdlib/string-base-repeat
[@stdlib/string/base/replace-after-last]: https://www.npmjs.com/package/@stdlib/string-base-replace-after-last
[@stdlib/string/base/replace-after]: https://www.npmjs.com/package/@stdlib/string-base-replace-after
[@stdlib/string/base/replace-before-last]: https://www.npmjs.com/package/@stdlib/string-base-replace-before-last
[@stdlib/string/base/replace-before]: https://www.npmjs.com/package/@stdlib/string-base-replace-before
[@stdlib/string/base/replace]: https://www.npmjs.com/package/@stdlib/string-base-replace
[@stdlib/string/base/reverse-code-points]: https://www.npmjs.com/package/@stdlib/string-base-reverse-code-points
[@stdlib/string/base/reverse-grapheme-clusters]: https://www.npmjs.com/package/@stdlib/string-base-reverse-grapheme-clusters
[@stdlib/string/base/reverse]: https://www.npmjs.com/package/@stdlib/string-base-reverse
[@stdlib/string/base/right-pad]: https://www.npmjs.com/package/@stdlib/string-base-right-pad
[@stdlib/string/base/right-trim]: https://www.npmjs.com/package/@stdlib/string-base-right-trim
[@stdlib/string/base/slice-code-points]: https://www.npmjs.com/package/@stdlib/string-base-slice-code-points
[@stdlib/string/base/slice-grapheme-clusters]: https://www.npmjs.com/package/@stdlib/string-base-slice-grapheme-clusters
[@stdlib/string/base/slice]: https://www.npmjs.com/package/@stdlib/string-base-slice
[@stdlib/string/base/snakecase]: https://www.npmjs.com/package/@stdlib/string-base-snakecase
[@stdlib/string/base/startcase]: https://www.npmjs.com/package/@stdlib/string-base-startcase
[@stdlib/string/base/starts-with]: https://www.npmjs.com/package/@stdlib/string-base-starts-with
[@stdlib/string/base/stickycase]: https://www.npmjs.com/package/@stdlib/string-base-stickycase
[@stdlib/string/base/trim]: https://www.npmjs.com/package/@stdlib/string-base-trim
[@stdlib/string/base/truncate-middle]: https://www.npmjs.com/package/@stdlib/string-base-truncate-middle
[@stdlib/string/base/uncapitalize]: https://www.npmjs.com/package/@stdlib/string-base-uncapitalize
[@stdlib/string/base/uppercase]: https://www.npmjs.com/package/@stdlib/string-base-uppercase