String
- 一、String
- 1_.camelCase([string=''])
- 2_.capitalize([string=''])
- 3_.endsWith([string=''], [target], [position=string.length])
- 4_.escape([string=''])
- 5_.kebabCase([string=''])
- 6_.pad([string=''], [length=0], [chars=' '])
- 7_.repeat([string=''], [n=1])
- 8_.replace([string=''], pattern, replacement)
- 9_.snakeCase([string=''])
- 10_.startCase([string=''])
- 11_.trim([string=''], [chars=whitespace])
- 12_.truncate([string=''], [options={}])
- 13_.words([string=''], [pattern])
- 14_.template([string=''], [options={}])
一、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>');
//<h3>123</h3>
_.unescape([string=’’])
_.escape的反向版。 这个方法转换string字符串中的 HTML 实体 &, <, >, ", ', 和 ` 为对应的字符。
_.unescape('<h3>123</h3>');
//<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!'