Lodash - truncate 方法
语法
_.truncate([string=''], [options={}])
如果字符串长度超过给定的最大字符串长度,则截断该字符串。截断字符串的最后一个字符将替换为省略字符串,默认为"..."。
参数
[string=''] (string) − 要截断的字符串。
[options={}] (Object) − 选项对象。
[options.length=30] (number) −最大字符串长度。
[options.omission='...'] (string) − 表示省略文本的字符串。
[options.separator] (RegExp|string) − 要截断的分隔符模式。
输出
(string) − 返回截断的字符串。
示例
var _ = require('lodash'); var result = _.truncate('Hi, this is a long text to be truncated', { 'length': 24, 'separator': ' ' }); console.log(result);
将上述程序保存在tester.js中。运行以下命令执行该程序。
命令
\>node tester.js
输出
Hi, this is a long...