截断字符串并在超长时添加省略号
Truncate a string and append an ellipsis when it exceeds the specified maximum length.
规则:
str.length
maxLength
ellipsis
str
Rules:
ellipsis.length
待处理的字符串 / input string
最大长度(包含省略号)/ maximum length including ellipsis
Optional
省略符,默认为 '...' / ellipsis string, default '...'
截断并可能包含省略号的字符串 / truncated string possibly with ellipsis
// truncate('abcdef', 4) -> 'a...'// truncate('hello', 10) -> 'hello' Copy
// truncate('abcdef', 4) -> 'a...'// truncate('hello', 10) -> 'hello'
截断字符串并在超长时添加省略号
Truncate a string and append an ellipsis when it exceeds the specified maximum length.
规则:
str.length小于或等于maxLength则返回原字符串。maxLength小于或等于 0 则返回空字符串。maxLength小于等于ellipsis的长度,则返回ellipsis的前maxLength个字符(保证结果长度不超过 maxLength)。str的前 (maxLength - ellipsis.length) 个字符 +ellipsis。Rules:
str.length<=maxLength, return original string.maxLength<= 0, return an empty string.maxLength<=ellipsis.length, return the firstmaxLengthcharacters ofellipsis.strplusellipsis.