About stdlib...
We 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!
Base String
Base (i.e., lower-level) string functions.
Installation
npm install @stdlib/string-base
Usage
var string = require( '@stdlib/string-base' );
string
Namespace containing "base" (i.e., lower-level) string functions.
var ns = string;
// returns {...}
The namespace contains the following functions:
-
altcase( str )
: convert a string to alternate case. -
camelcase( str )
: convert a string to camel case. -
capitalize( str )
: capitalize the first character in a string. -
codePointAt( string, position, backward )
: return a Unicode code point from a string at a specified position. -
constantcase( str )
: convert a string to constant case. -
distances
: implementations of various string similarity metrics. -
dotcase( str )
: convert a string to dot case. -
endsWith( str, search, len )
: test if a string ends with the characters of another string. -
firstCodePoint( str, n )
: return the firstn
Unicode code points of a string. -
firstGraphemeCluster( str, n )
: return the firstn
grapheme clusters (i.e., user-perceived characters) of a string. -
first( str, n )
: return the firstn
UTF-16 code units of a string. -
forEachCodePoint( str, clbk[, thisArg ] )
: invokes a function for each Unicode code point in a string. -
forEachGraphemeCluster( str, clbk[, thisArg ] )
: invokes a function for each grapheme cluster (i.e., user-perceived character) in a string. -
forEach( str, clbk[, thisArg ] )
: invokes a function for each UTF-16 code unit in a string. -
formatInterpolate( tokens, ...args )
: generate string from a token array by interpolating values. -
formatTokenize( str )
: tokenize a string into an array of string parts and format identifier objects. -
headercase( str )
: convert a string to HTTP header case. -
invcase( str )
: convert a string to inverse case. -
kebabcase( str )
: convert a string to kebab case. -
lpad( str, len, pad )
: left pad a string. -
ltrim( str )
: trim whitespace characters from the beginning of a string. -
lowercase( str )
: convert a string to lowercase. -
pascalcase( str )
: convert a string to Pascal case. -
percentEncode( str )
: percent-encode a UTF-16 encoded string according to RFC 3986. -
removeFirstCodePoint( str, n )
: remove the firstn
Unicode code points of a string. -
removeFirstGraphemeCluster( str, n )
: remove the firstn
grapheme clusters (i.e., user-perceived characters) of a string. -
removeFirst( str, n )
: remove the firstn
UTF-16 code units of a string. -
removeLastCodePoint( str, n )
: remove the lastn
Unicode code points of a string. -
removeLastGraphemeCluster( str, n )
: remove the lastn
grapheme clusters (i.e., user-perceived characters) of a string. -
removeLast( str, n )
: remove the lastn
UTF-16 code units of a string. -
repeat( str, n )
: repeat a string a specified number of times and return the concatenated result. -
replaceBefore( str, search, replacement )
: replace the substring before the first occurrence of a specified search string. -
replace( str, search, newval )
: replace search occurrences with a replacement string. -
reverseCodePoints( str )
: reverse the Unicode code points of a string. -
reverseGraphemeClusters( str )
: reverse the grapheme clusters (i.e., user-perceived characters) of a string. -
reverse( str )
: reverse the UTF-16 code units of a string. -
rpad( str, len, pad )
: right pad a string. -
rtrim( str )
: trim whitespace characters from the end of a string. -
snakecase( str )
: convert a string to snake case. -
startcase( str )
: capitalize the first letter of each word in a string. -
startsWith( str, search, position )
: test if a string starts with the characters of another string. -
trim( str )
: trim whitespace characters from the beginning and end of a string. -
truncateMiddle( str, len, seq )
: truncate the middle UTF-16 code units of a string to return a string having a specified length. -
uncapitalize( str )
: uncapitalize the first character of a string. -
uppercase( str )
: convert a string to uppercase.
Examples
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'
Notice
This package is part of 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, see the main project repository.
Community
License
See LICENSE.
Copyright
Copyright © 2016-2024. The Stdlib Authors.