본문으로 건너뛰기

truncate

긴 문자열을 지정된 길이로 잘라내고, 문자열 뒤에 선택적으로 줄임표를 추가합니다.

길이는 코드 포인트 단위로 계산되므로, 기본 다국어 평면(BMP) 밖의 문자도 모든 언어에서 1자로 세어지며 중간에서 잘리지 않습니다.

Parameters

이름타입필수기본값
strstringStringstr
자를 문자열입니다.
lengthnumberint
잘라낼 길이이며, 코드 포인트 단위로 계산됩니다.
ellipsisnamedkeywordstringStringstr''
문자열 뒤에 덧붙일 값이며, 실제로 잘렸을 때만 추가됩니다.

named이 파라미터는 named 파라미터로 전달합니다.

keyword이 파라미터는 키워드 인자로 전달하거나, 대신 dict 하나로 전달합니다.

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