> For the complete documentation index, see [llms.txt](https://lodash.shujuwajue.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lodash.shujuwajue.com/string/truncate.md).

# \_.truncate（截断字符串）

**语法：**

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

**源代码链接：**[source](https://github.com/lodash/lodash/blob/4.17.10/lodash.js#L15068)

**npm包链接：**[npm package](https://www.npmjs.com/package/lodash.truncate)

**描述：**

截断`string`字符串，如果字符串超出了限定的最大值。 被截断的字符串后面会以 omission 代替，omission 默认是 "..."。

**开始版本：**&#x34;.0.0

**参数：**

* `[string=''] (string)`: 要截断的字符串。
* `[options={}] (Object)`: 选项对象。
* `[options.length=30] (number)`: 允许的最大长度。
* `[options.omission='...'] (string)`: 超出后的代替字符。
* `[options.separator] (RegExp|string)`: 截断点。

**返回值：**

* `(string)`: 返回处理后的字符串。

**例子：**

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