# 字符串

返回lodash

# _.camelCase([string=''])

_.camelCase('Foo Bar');
// => 'fooBar'
 
_.camelCase('--foo-bar--');
// => 'fooBar'
 
_.camelCase('__FOO_BAR__');
// => 'fooBar'
1
2
3
4
5
6
7
8

# _.kebabCase([string=''])

转换字符串string为kebab case.

_.kebabCase('Foo Bar');
// => 'foo-bar'
 
_.kebabCase('fooBar');
// => 'foo-bar'
 
_.kebabCase('__FOO_BAR__');
// => 'foo-bar'
1
2
3
4
5
6
7
8

# _.snakeCase([string=''])

_.snakeCase('Foo Bar');
// => 'foo_bar'
 
_.snakeCase('fooBar');
// => 'foo_bar'
 
_.snakeCase('--FOO-BAR--');
// => 'foo_bar'
1
2
3
4
5
6
7
8

# _.startCase([string=''])

_.startCase('--foo-bar--');
// => 'Foo Bar'
 
_.startCase('fooBar');
// => 'Foo Bar'
 
_.startCase('__FOO_BAR__');
// => 'FOO BAR'
1
2
3
4
5
6
7
8

# _.capitalize([string=''])

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

_.capitalize('FRED');
// => 'Fred'
1
2

# _.lowerCase([string=''])

转换字符串string以空格分开单词,并转换为小写。

_.lowerCase('--Foo-Bar--');
// => 'foo bar'
 
_.lowerCase('fooBar');
// => 'foo bar'
 
_.lowerCase('__FOO_BAR__');
// => 'foo bar'
1
2
3
4
5
6
7
8

# _.words([string=''], [pattern])

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

  • [string=''] (string): 要拆分的字符串。
  • [pattern] (RegExp|string): 匹配模式。
_.words('fred, barney, & pebbles');
// => ['fred', 'barney', 'pebbles']
 
_.words('fred, barney, & pebbles', /[^, ]+/g);
// => ['fred', 'barney', '&', 'pebbles']
1
2
3
4
5

# _.lowerFirst([string=''])

转换字符串string的首字母为小写。

_.lowerFirst('Fred');
// => 'fred'
 
_.lowerFirst('FRED');
// => 'fRED'
1
2
3
4
5

# _.toLower([string=''])

转换整个string字符串的字符为小写,类似String#toLowerCase

_.toLower('--Foo-Bar--');
// => '--foo-bar--'
 
_.toLower('fooBar');
// => 'foobar'
 
_.toLower('__FOO_BAR__');
// => '__foo_bar__'
1
2
3
4
5
6
7
8

# _.toUpper([string=''])

_.toUpper('--foo-bar--');
// => '--FOO-BAR--'
 
_.toUpper('fooBar');
// => 'FOOBAR'
 
_.toUpper('__foo_bar__');
// => '__FOO_BAR__'
1
2
3
4
5
6
7
8

# _.parseInt(string, [radix=10])

转换string字符串为指定基数的整数。
如果基数是 undefined 或者 0,则radix基数默认是10,
如果string字符串是16进制,则radix基数为 16。

注意:

这个方法与ES5 implementation 的 parseInt是一样的。

_.parseInt('08');
// => 8
 
_.map(['6', '08', '10'], _.parseInt);
// => [6, 8, 10]
1
2
3
4
5

# _.deburr([string=''])

转换字符串string中拉丁语-1补充字母拉丁语扩展字母-A 为基本的拉丁字母,并且去除组合变音标记。

# _.startsWith([string=''], [target], [position=0])

_.startsWith('abc', 'a');
// => true
 
_.startsWith('abc', 'b');
// => false
 
_.startsWith('abc', 'b', 1);
// => true
1
2
3
4
5
6
7
8

# _.endsWith([string=''], [target], [position=string.length])

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

  • [string=''] (string): 要检索的字符串。
  • [target] (string): 要检索字符。
  • [position=string.length] (number): 检索的位置。
_.endsWith('abc', 'c');
// => true
 
_.endsWith('abc', 'b');
// => false
 
_.endsWith('abc', 'b', 2);
// => true
1
2
3
4
5
6
7
8

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

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

_.pad('abc', 8);
// => '  abc   '
 
_.pad('abc', 8, '_-');
// => '_-abc_-_'
 
_.pad('abc', 3);
// => 'abc'
1
2
3
4
5
6
7
8

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

如果string字符串长度小于 length 则在右侧填充字符。 如果超出length长度则截断超出的部分。

_.padEnd('abc', 6);
// => 'abc   '
 
_.padEnd('abc', 6, '_-');
// => 'abc_-_'
 
_.padEnd('abc', 3);
// => 'abc'
1
2
3
4
5
6
7
8

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

_.padStart('abc', 6);
// => '   abc'
 
_.padStart('abc', 6, '_-');
// => '_-_abc'
 
_.padStart('abc', 3);
// => 'abc'
1
2
3
4
5
6
7
8

# _.replace([string=''], pattern, replacement)

_.replace('Hi Fred', 'Fred', 'Barney');
// => 'Hi Barney'
1
2

# _.repeat([string=''], [n=1])

_.repeat('*', 3);
// => '***'
 
_.repeat('abc', 2);
// => 'abcabc'
 
_.repeat('abc', 0);
// => ''
1
2
3
4
5
6
7
8

# _.split([string=''], separator, [limit])

  • [string=''] (string): 要拆分的字符串。
  • separator (RegExp|string): 拆分的分隔符。
  • [limit] (number): 限制结果的数量。
_.split('a-b-c', '-', 2);
// => ['a', 'b']
1
2

# _.truncate([string=''], [options=])

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

  • [string=''] (string): 要截断的字符串。
  • [options=] (Object): 选项对象。
    • [options.length=30] (number): 允许的最大长度。
    • [options.omission='...'] (string): 超出后的代替字符。
    • [options.separator] (RegExp|string): 截断点。
_.truncate('hi-diddly-ho there, neighborino');
// => 'hi-diddly-ho there, neighbo...'
 
_.truncate('hi-diddly-ho there, neighborino', {
  'length': 24,
  'separator': ' '
});
// => 'hi-diddly-ho there,...'
 
_.truncate('hi-diddly-ho there, neighborino', {
  'length': 24,
  'separator': /,? +/
});
// => 'hi-diddly-ho there...'
 
_.truncate('hi-diddly-ho there, neighborino', {
  'omission': ' [...]'
});
// => 'hi-diddly-ho there, neig [...]'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19