主题
truncate
js
_.truncate([string=''], [options={}])如果 string 长于给定的最大字符串长度,则截断它。截断字符串的最后一个字符被替换为省略字符串,默认为 "..."。
¥Truncates string if it's longer than the given maximum string length. The last characters of the truncated string are replaced with the omission string which defaults to "...".
新增于
¥Since
4.0.0
参数
¥Arguments
[string=''](字符串):要截断的字符串。¥
[string=''](string): The string to truncate.[options={}](对象):填充长度。¥
[options={}](Object): The options object.[options.length=30](数值):要检查的数字。¥
[options.length=30](number): The maximum string length.[options.omission='...'](字符串):表示省略文本的字符串。¥
[options.omission='...'](string): The string to indicate text is omitted.[options.separator](正则表达式|字符串):要截断的分隔符模式。¥
[options.separator](RegExp|string): The separator pattern to truncate to.
返回
¥Returns
(字符串):返回截断的字符串。
¥(string): Returns the truncated string.
示例
¥Example
js
_.truncate('hi-diddly-ho there, neighborino');
// => 'hi-diddly-ho there, neighbo...'
_.truncate('hi-diddly-ho there, neighborino', {
'length': 24,
'separator': ' '
});
// => 'hi-diddly-ho there,...'
_.truncate('hi-diddly-ho there, neighborino', {
'length': 24,
'separator': /,? +/
});
// => 'hi-diddly-ho there...'
_.truncate('hi-diddly-ho there, neighborino', {
'omission': ' [...]'
});
// => 'hi-diddly-ho there, neig [...]'