Lodash - pad 方法



語法

_.pad([string=''], [length=0], [chars=' '])

如果字串的長度小於 length,則在字串的左側和右側填充。如果填充字元不能被 length 均勻整除,則截斷填充字元。

引數

  • [string=''] (string) − 待填充的字串。

  • [length=0] (number) − 填充長度。

  • [chars=' '] (string) − 用作填充的字串。

輸出

  • (string) − 返回填充後的字串。

示例

var _ = require('lodash');
var result = _.pad('foo', 8, '_');

console.log(result);

將以上程式儲存在 tester.js 中。執行以下命令來執行此程式。

命令

\>node tester.js

輸出

__foo___
lodash_string.htm
廣告
© . All rights reserved.