Lodash工具库Srting

一、String

1_.camelCase([string=’’])

转换字符串string为 驼峰写法。

_.camelCase('Foo Bar');//'fooBar'

2_.capitalize([string=’’])

转换字符串string首字母为大写,剩下为小写。

_.capitalize('FRED');//'Fred'

3_.endsWith([string=’’], [target], [position=string.length])

检查字符串string是否以给定的target字符串结尾。

_.endsWith('abc', 'c');//true

4_.escape([string=’’])

转义string中的 “&”, “<”, “>”, ‘"’, “’”, 和 “`” 字符为HTML实体字符。

_.escape('<h3>123</h3>');
//&lt;h3&gt;123&lt;/h3&gt;

_.unescape([string=’’])
_.escape的反向版。 这个方法转换string字符串中的 HTML 实体 &, <, >, ", ', 和 ` 为对应的字符。

_.unescape('&lt;h3&gt;123&lt;/h3&gt');
//<h3>123</h3>

5_.kebabCase([string=’’])

转换字符串string为 kebab case.

_.kebabCase('Foo Bar');//'foo-bar'

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

如果string字符串长度小于 length 则从左侧和右侧填充字符。 如果没法平均分配,则截断超出的长度。

_.pad('abc', 8, '_');

_.padEnd([string=’’], [length=0], [chars=’ '])
如果string字符串长度小于 length 则在右侧填充字符。 如果超出length长度则截断超出的部分。
_.padStart([string=’’], [length=0], [chars=’ '])
如果string字符串长度小于 length 则在左侧填充字符。 如果超出length长度则截断超出的部分。

7_.repeat([string=’’], [n=1])

重复 N 次给定字符串。

_.repeat('abc', 2);//'abcabc'

8_.replace([string=’’], pattern, replacement)

替换string字符串中匹配的pattern为给定的replacement 。

_.replace('Hi Fred', 'Fred', 'Barney');//'Hi Barney'

9_.snakeCase([string=’’])

转换字符串string为 snake case.

_.snakeCase('Foo Bar');//'foo_bar'

10_.startCase([string=’’])

转换 string 字符串为 start case.

_.startCase(' foo-bar');
// => 'Foo Bar'

11_.trim([string=’’], [chars=whitespace])

从string字符串中移除前面和后面的 空格 或 指定的字符。

_.trim('abc-_-', '-_-');//'abc'

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

截断string字符串,如果字符串超出了限定的最大值。 被截断的字符串后面会以 omission 代替,omission 默认是 “…”。

_.truncate('hi-diddly-ho there, neighborino');//'hi-diddly-ho there, neighbo...'
_.truncate('hi-diddly-ho there, neighborino', {
  'length': 24,
});//'hi-diddly-ho there,...'

13_.words([string=’’], [pattern])

拆分字符串string中的词为数组 。

_.words('fred, barney, & pebbles');//['fred', 'barney', 'pebbles']

14_.template([string=’’], [options={}])

创建一个预编译模板方法,可以插入数据到模板中 “interpolate” 分隔符相应的位置。

var compiled = _.template('hello <%= user %>!');
compiled({ 'user': 'fred' });//'hello fred!'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞羽逐星

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值