@stdlib/string-truncate-middle
v0.2.2
Published
Truncate a string in the middle to a specified length.
Downloads
1,468
Readme
truncateMiddle
Truncate a string in the middle to a specified length.
Installation
npm install @stdlib/string-truncate-middle
Usage
var truncateMiddle = require( '@stdlib/string-truncate-middle' );
truncate( str, len[, seq] )
Truncates a string in the middle to a specified length.
var out = truncateMiddle( 'beep boop', 7 );
// returns 'be...op'
By default, the truncated string uses the replacement sequence '...'
. To customize the replacement sequence, provide a seq
argument:
var out = truncateMiddle( 'beep boop', 7, '!' );
// returns 'bee!oop'
out = truncateMiddle( 'beep boop', 7, '!!!' );
// returns 'be!!!op'
Examples
var truncateMiddle = require( '@stdlib/string-truncate-middle' );
var str = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
var out = truncateMiddle( str, 15 );
// returns 'Lorem ... elit.'
str = 'To be or not to be, that is the question';
out = truncateMiddle( str, 19, '|' );
// returns 'To be or | question'
str = 'The quick fox jumps over the lazy dog.';
out = truncateMiddle( str, 28, '...' );
// returns 'The quick fox...he lazy dog.'
str = '🐺 Wolf Brothers 🐺';
out = truncateMiddle( str, 7 );
// returns '🐺 ... 🐺'
str = '🐺 Wolf Pack 🐺';
out = truncateMiddle( str, 7, '🐺🐺🐺' );
// returns '🐺 🐺🐺🐺 🐺'
See Also
@stdlib/string-truncate-middle-cli
: CLI package for use as a command-line utility.@stdlib/string-truncate
: truncate a string to a specified length.
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.