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
| Name | Type | Required | Default |
|---|---|---|---|
str | stringStringstr | ● | – |
| The string to truncate. | |||
length | numberint | ● | – |
| The length to truncate at, counted in code points. | |||
ellipsis named keyword | stringStringstr | – | '' |
| 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...'