Skip to content

truncateExpect

The string ignores truncation until the ending character (endStringChar). If the expected length is reached, return the truncated string until after the ending character.

Several ending characters may be given as an array, and the whole sentence that crosses the expected length is still kept.

The default is the full stop as each script writes it — ., (ideographic), (fullwidth) and (halfwidth) — so Japanese and Chinese text is cut at its own period instead of being returned untouched. ! and ? are deliberately left out: an ASCII ! has never ended a sentence here, so accepting would split the same text differently depending on the script it is written in. Pass ['.', '!', '?', '。', '!', '?'] to opt in.

A longer ending character is matched before a shorter one, so . next to ... does not cut ... short. When no ending character is found, or the list is empty, the string is returned untouched.

expectLength is counted in code points, so a character outside the Basic Multilingual Plane counts as one in every language.

Parameters

NameTypeRequiredDefault
strstringStringstr
The string to truncate.
expectLengthnumberint
The length to truncate at, counted in code points. The sentence that crosses it is kept whole.
endStringCharnamedkeywordstring | string[]dynamicstr | list[str]['.', '。', '.', '。']
The ending character, or an array of them. A longer one is matched first, and an empty list returns the string untouched.

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
truncateExpect('hello. this is test string.', 10, '.'); // Returns 'hello. this is test string.'
truncateExpect('hello-this-is-test-string-bye', 14, '-'); // Returns 'hello-this-is-'
truncateExpect('これはテストです。よろしくお願いします。さようなら。', 10); // Returns 'これはテストです。よろしくお願いします。'
truncateExpect('你好。这是测试。再见。', 5); // Returns '你好。这是测试。'
truncateExpect('a. b! c? d.', 4, ['.', '!', '?']); // Returns 'a. b!'
dart
truncateExpect('hello. this is test string.', 10, endStringChar: '.'); // Returns 'hello. this is test string.'
truncateExpect('hello-this-is-test-string-bye', 14, endStringChar: '-'); // Returns 'hello-this-is-'
truncateExpect('これはテストです。よろしくお願いします。さようなら。', 10); // Returns 'これはテストです。よろしくお願いします。'
truncateExpect('你好。这是测试。再见。', 5); // Returns '你好。这是测试。'
truncateExpect('a. b! c? d.', 4, endStringChar: ['.', '!', '?']); // Returns 'a. b!'
python
truncateExpect('hello. this is test string.', 10, '.')  # Returns 'hello. this is test string.'
truncateExpect('hello-this-is-test-string-bye', 14, '-')  # Returns 'hello-this-is-'
truncateExpect('これはテストです。よろしくお願いします。さようなら。', 10)  # Returns 'これはテストです。よろしくお願いします。'
truncateExpect('你好。这是测试。再见。', 5)  # Returns '你好。这是测试。'
truncateExpect('a. b! c? d.', 4, ['.', '!', '?'])  # Returns 'a. b!'

Released under the MIT License