Skip to content

truncate

Truncates a long string to a specified length, optionally appending an ellipsis after the string.

Length is counted in code points, so a character outside the Basic Multilingual Plane counts as one in every language and is never cut in half.

Parameters

NameTypeRequiredDefault
strstringStringstr
The string to truncate.
lengthnumberint
The length to truncate at, counted in code points.
ellipsisnamedkeywordstringStringstr''
Appended after the string, but only when it was actually truncated.

namedThese parameters are passed as named parameters.

keywordThese parameters are passed as keyword arguments, or as a single dict in their place.

Returns

string

String

str

Examples

javascript
truncate('hello', 3); // Returns 'hel'
truncate('hello', 2, '...'); // Returns 'he...'
dart
truncate('hello', 3); // Returns 'hel'
truncate('hello', 2, ellipsis: '...'); // Returns 'he...'
python
truncate('hello', 3)  # Returns 'hel'
truncate('hello', 2, '...')  # Returns 'he...'

Released under the MIT License