Skip to content

@longmo-utils/common / truncate

Function: truncate()

ts
function truncate(
   str, 
   length, 
   suffix): string;

截断字符串到指定长度

Parameters

ParameterTypeDefault valueDescription
strstringundefined输入字符串
lengthnumberundefined最大长度
suffixstring'...'截断时添加的后缀(默认: '...')

Returns

string

截断后的字符串

Example

ts
truncate('hello world', 5) // 'hello...'
truncate('hello world', 11) // 'hello world'
truncate('hello world', 5, '---') // 'hello---'

Released under the MIT License.