@stdlib/string-last
v0.1.0
Published
Return the last character(s) of a string.
Downloads
5
Readme
last
Return the last character(s) of a string.
Installation
npm install @stdlib/string-last
Usage
var last = require( '@stdlib/string-last' );
last( str[, n][, options] )
Returns the last character(s) of a string.
var out = last( 'last man standing' );
// returns 'g'
out = last( 'Hidden Treasures' );
// returns 's'
The function supports the following options:
mode: type of characters to return. Must be one of the following:
'grapheme'
: grapheme clusters. Appropriate for strings containing visual characters which can span multiple Unicode code points (e.g., emoji).'code_point'
: Unicode code points. Appropriate for strings containing visual characters which are comprised of more than one Unicode code unit (e.g., ideographic symbols and punctuation and mathematical alphanumerics).'code_unit'
: UTF-16 code units. Appropriate for strings containing visual characters drawn from the basic multilingual plane (BMP) (e.g., common characters, such as those from the Latin, Greek, and Cyrillic alphabets).
Default:
'grapheme'
.
By default, the function returns the last character. To return the last n
characters, provide a second argument specifying the number of characters to return.
var out = last( 'foo bar', 3 );
// returns 'bar'
out = last( 'foo bar', 10 );
// returns 'foo bar'
Notes
- By default, the function assumes the general case in which an input string may contain an arbitrary number of grapheme clusters. This assumption comes with a performance cost. Accordingly, if an input string is known to only contain visual characters of a particular type (e.g., only alphanumeric), one can achieve better performance by specifying the appropriate
mode
option.
Examples
var last = require( '@stdlib/string-last' );
var str = last( 'last man standing' );
// returns 'g'
str = last( 'presidential election' );
// returns 'n'
str = last( 'javaScript' );
// returns 't'
str = last( 'Hidden Treasures' );
// returns 's'
str = last( 'The Last of the Mohicans', 8 );
// returns 'Mohicans'
str = last( '🐶🐮🐷🐰🐸', 2 );
// returns '🐰🐸'
str = last( '🐶🐮🐷🐰🐸', 10 );
// returns '🐶🐮🐷🐰🐸'
See Also
@stdlib/string-last-cli
: CLI package for use as a command-line utility.
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.