4.17.11
3.10.1
2.4.2
1.3.1

_.truncate([string=''], [options={}])

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

argument
[string='']
[options={}]
[options.length=30]
[options.omission='...']
[options.separator]
type
string
Object
number
string
(RegExp|string)
description
The string to truncate.
The options object.
The maximum string length.
The string to indicate text is omitted.
The separator pattern to truncate to.

Returns

(array)

Example

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