Skip to content

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

  1. [string=''] (字符串):要截断的字符串。

    ¥[string=''] (string): The string to truncate.

  2. [options={}] (对象):填充长度。

    ¥[options={}] (Object): The options object.

  3. [options.length=30] (数值):要检查的数字。

    ¥[options.length=30] (number): The maximum string length.

  4. [options.omission='...'] (字符串):表示省略文本的字符串。

    ¥[options.omission='...'] (string): The string to indicate text is omitted.

  5. [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 [...]'

Lodash v4.17 中文网 - 粤ICP备13048890号