字符串

返回lodash

_.camelCase([string=''])

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

_.camelCase('--foo-bar--');
// => 'fooBar'

_.camelCase('__FOO_BAR__');
// => 'fooBar'

_.kebabCase([string=''])

转换字符串string为kebab case.

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

_.kebabCase('fooBar');
// => 'foo-bar'

_.kebabCase('__FOO_BAR__');
// => 'foo-bar'

_.snakeCase([string=''])

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

_.snakeCase('fooBar');
// => 'foo_bar'

_.snakeCase('--FOO-BAR--');
// => 'foo_bar'

_.startCase([string=''])

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

_.startCase('fooBar');
// => 'Foo Bar'

_.startCase('__FOO_BAR__');
// => 'FOO BAR'

_.capitalize([string=''])

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

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

_.lowerCase([string=''])

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

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

_.lowerCase('fooBar');
// => 'foo bar'

_.lowerCase('__FOO_BAR__');
// => 'foo bar'

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

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

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

_.words('fred, barney, & pebbles', /[^, ]+/g);
// => ['fred', 'barney', '&', 'pebbles']

_.lowerFirst([string=''])

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

_.lowerFirst('Fred');
// => 'fred'

_.lowerFirst('FRED');
// => 'fRED'

_.toLower([string=''])

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

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

_.toLower('fooBar');
// => 'foobar'

_.toLower('__FOO_BAR__');
// => '__foo_bar__'

_.toUpper([string=''])

_.toUpper('--foo-bar--');
// => '--FOO-BAR--'

_.toUpper('fooBar');
// => 'FOOBAR'

_.toUpper('__foo_bar__');
// => '__FOO_BAR__'

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

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

::: danger 注意: 这个方法与ES5 implementation 的 parseInt是一样的。 :::

_.parseInt('08');
// => 8

_.map(['6', '08', '10'], _.parseInt);
// => [6, 8, 10]

_.deburr([string=''])

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

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

_.startsWith('abc', 'a');
// => true

_.startsWith('abc', 'b');
// => false

_.startsWith('abc', 'b', 1);
// => true

_.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

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

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

_.pad('abc', 8);
// => '  abc   '

_.pad('abc', 8, '_-');
// => '_-abc_-_'

_.pad('abc', 3);
// => 'abc'

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

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

_.padEnd('abc', 6);
// => 'abc   '

_.padEnd('abc', 6, '_-');
// => 'abc_-_'

_.padEnd('abc', 3);
// => 'abc'

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

_.padStart('abc', 6);
// => '   abc'

_.padStart('abc', 6, '_-');
// => '_-_abc'

_.padStart('abc', 3);
// => 'abc'

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

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

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

_.repeat('*', 3);
// => '***'

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

_.repeat('abc', 0);
// => ''

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

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

_.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 [...]'