Lodash - truncate 方法
語法
_.truncate([string=''], [options={}])
如果字串長度超過指定的最大字串長度,則截斷字串。截斷字串的最後一個字元將替換為省略號字串,預設為“...”。
引數
[string=''] (字串) − 要截斷的字串。
[options={}] (物件) − 選項物件。
[options.length=30] (數字) − 最大字串長度。
[options.omission='...'] (字串) − 指示省略文字的字串。
[options.separator] (正則表示式或字串) − 要截斷的分隔符模式。
輸出
(字串) − 返回截斷的字串。
示例
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...
lodash_string.htm
廣告