字符串 String Methods

Lodash v4 字符串的使用方法

_.camelCase

string 转换为 camel case

引入版本 3.0.0

source / npm package

参数

  1. [string=''] (string): 要转换的字符串。

返回

(string): 返回骆驼大小写的字符串。

示例

js
_.camelCase('Foo Bar')
_.camelCase('--foo-bar--')
_.camelCase('__FOO_BAR__')

_.capitalize

string 的第一个字符转换为大写,将其余字符转换为小写。

引入版本 3.0.0

source / npm package

参数

  1. [string=''] (string): 要大写的字符串。

返回

(string): 返回大写的字符串。

示例

_.endsWith

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

引入版本 3.0.0

source / npm package

参数 ([string=''], [target], [position=string.length])

  1. [string=''] (string): 要检查的字符串。
  2. [target] (string): 要搜索的字符串。
  3. [position=string.length] (number): 要搜索的位置。

返回

(boolean): 如果 stringtarget 结尾,则返回 true,否则返回 false

示例

js
_.endsWith('abc', 'c')
_.endsWith('abc', 'b')
_.endsWith('abc', 'b', 2)

_.escape

string 中的字符 "&"、"<"、">"、'"' 和 "'" 转换为其对应的 HTML 实体。

注意: 没有其他字符被转义。 要转义其他字符,请使用 he 等第三方库。

尽管 ">" 字符为了对称而进行了转义,但像 ">" 和 "/" 这样的字符在 HTML 中不需要转义并且没有特殊含义,除非它们是标记或未引用属性值的一部分。 请参阅 Mathias Bynens 的文章_(在“半相关的有趣事实”下)_了解更多详细信息。

使用 HTML 时,您应该始终 引用属性值 以减少 XSS 向量。

引入版本 0.1.0

source / npm package

参数

  1. [string=''] (string): 要转义的字符串。

返回

(string): 返回转义的字符串。

示例

js
_.escape('fred, barney, & pebbles')

_.escapeRegExp

转义 RegExp 特殊字符 "^"、"$"、""、"."、"*"、"+"、"?"、"("、")"、"["、"\ ]”、“{”、“}”和“|” 在“字符串”中。

引入版本 3.0.0

source / npm package

参数

  1. [string=''] (string): 要转义的字符串。

返回

(string): 返回转义的字符串。

示例

js
_.escapeRegExp('[lodash](https://lodash.com/)')

_.kebabCase

Converts string to kebab case.

引入版本 3.0.0

source / npm package

参数

  1. [string=''] (string): 要转换的字符串。

返回

(string): 返回 kebab 大小写的字符串。

示例

js
_.kebabCase('Foo Bar')
_.kebabCase('fooBar')
_.kebabCase('__FOO_BAR__')

_.lowerCase

将作为空格分隔的单词的“字符串”转换为小写。

引入版本 4.0.0

source / npm package

参数

  1. [string=''] (string): 要转换的字符串。

返回

(string): 返回小写字符串。

示例

js
_.lowerCase('--Foo-Bar--')
_.lowerCase('fooBar')
_.lowerCase('__FOO_BAR__')

_.lowerFirst

string 的第一个字符转换为小写。

引入版本 4.0.0

source / npm package

参数

  1. [string=''] (string): 要转换的字符串。

返回

(string): 返回转换后的字符串。

示例

js
_.lowerFirst('Fred')
_.lowerFirst('FRED')

_.pad

如果字符串短于 length,则在左侧和右侧填充 string。 如果填充字符不能被 length 均分,则它们将被截断。

引入版本 3.0.0

source / npm package

参数 ([string=''], [length=0], [chars=' '])

  1. [string=''] (string): 要填充的字符串。
  2. [length=0] (number): 填充长度。
  3. [chars=' '] (string): 用作填充的字符串。

返回

(string): 返回填充的字符串。

示例

js
_.pad('abc', 8)
_.pad('abc', 8, '_-')
_.pad('abc', 3)

_.padEnd

如果字符串短于 length,则在右侧填充 string。 如果填充字符超过 length,它们将被截断。

引入版本 4.0.0

source / npm package

参数 ([string=''], [length=0], [chars=' '])

  1. [string=''] (string): 要填充的字符串。
  2. [length=0] (number): 填充长度。
  3. [chars=' '] (string): 用作填充的字符串。

返回

(string): 返回填充的字符串。

示例

js
_.padEnd('abc', 6)
_.padEnd('abc', 6, '_-')
_.padEnd('abc', 3)

_.padStart

如果字符串短于 length,则在左侧填充 string。 如果填充字符超过 length,它们将被截断。

引入版本 4.0.0

source / npm package

参数 ([string=''], [length=0], [chars=' '])

  1. [string=''] (string): 要填充的字符串。
  2. [length=0] (number): 填充长度。
  3. [chars=' '] (string): 用作填充的字符串。

返回

(string): 返回填充的字符串。

示例

js
_.padStart('abc', 6)
_.padStart('abc', 6, '_-')
_.padStart('abc', 3)

_.parseInt

string 转换为指定基数的整数。 如果 radixundefined0,则使用 10radix,除非 value 是十六进制,在这种情况下,使用 16radix

注意: 此方法与 parseIntES5 实现 一致。

引入版本 1.1.0

source / npm package

参数 (string, [radix=10])

  1. string (string): 要转换的字符串。
  2. [radix=10] (number): 解释 value 的基数。

返回

(number): 返回转换后的整数。

示例

js
_.parseInt('08')
_.map(['6', '08', '10'], _.parseInt)

_.repeat

重复给定的字符串“n”次。

引入版本 3.0.0

source / npm package

参数 ([string=''], [n=1])

  1. [string=''] (string): 要重复的字符串。
  2. [n=1] (number): 重复字符串的次数。

返回

(string): 返回重复的字符串。

示例

js
_.repeat('*', 3)
_.repeat('abc', 2)
_.repeat('abc', 0)

_.replace

replacement 替换 stringpattern 的匹配项。

注意: 此方法基于 String#replace

引入版本 4.0.0

source / npm package

参数 ([string=''], pattern, replacement)

  1. [string=''] (string): 要修改的字符串。
  2. pattern (RegExp|string): 要替换的模式。
  3. replacement (Function|string): 比赛替补。

返回

(string): 返回修改后的字符串。

示例

js
_.replace('Hi Fred', 'Fred', 'Barney')

_.snakeCase

Converts string to snake case.

引入版本 3.0.0

source / npm package

参数

  1. [string=''] (string): 要转换的字符串。

返回

(string): 返回蛇大小写的字符串。

示例

js
_.snakeCase('Foo Bar')
_.snakeCase('fooBar')
_.snakeCase('--FOO-BAR--')

_.split

通过 separator 拆分 string

注意: 此方法基于 String#split

引入版本 4.0.0

source / npm package

参数 ([string=''], separator, [limit])

  1. [string=''] (string): 要拆分的字符串。
  2. separator (RegExp|string): 要分割的分隔符模式。
  3. [limit] (number): 将结果截断到的长度。

返回

(Array): 返回字符串段。

示例

js
_.split('a-b-c', '-', 2)

_.startCase

Converts string to start case.

引入版本 3.1.0

source / npm package

参数

  1. [string=''] (string): 要转换的字符串。

返回

(string): 返回开始大小写的字符串。

示例

js
_.startCase('--foo-bar--')
_.startCase('fooBar')
_.startCase('__FOO_BAR__')

_.startsWith

检查 string 是否以给定的目标字符串开头。

引入版本 3.0.0

source / npm package

参数 ([string=''], [target], [position=0])

  1. [string=''] (string): 要检查的字符串。
  2. [target] (string): 要搜索的字符串。
  3. [position=0] (number): 要搜索的位置。

返回

(boolean): 如果 stringtarget 开头,则返回 true,否则返回 false

示例

js
_.startsWith('abc', 'a')
_.startsWith('abc', 'b')
_.startsWith('abc', 'b', 1)

_.template

创建一个编译的模板函数,可以在“interpolate”分隔符中插入数据属性,在“escape”分隔符中插入 HTML-escape 插入数据属性,并在“evaluate”分隔符中执行 JavaScript。 数据属性可以作为模板中的自由变量访问。 如果给出了设置对象,则它优先于 _.templateSettings 值。

注意: 在开发构建中 _.template 使用 [sourceURLs](http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/#toc -sourceurl) 以便于调试。

有关预编译模板的更多信息,请参阅 lodash 的自定义构建文档

有关 Chrome 扩展沙箱的更多信息,请参阅 Chrome 的扩展文档

引入版本 0.1.0

source / npm package

参数 ([string=''], [options={}])

  1. [string=''] (string): 模板字符串。
  2. [options={}] (Object): 选项对象。
  3. [options.escape=_.templateSettings.escape] (RegExp): HTML“转义”分隔符。
  4. [options.evaluate=_.templateSettings.evaluate] (RegExp): “评估”分隔符。
  5. [options.imports=_.templateSettings.imports] (Object): 作为自由变量导入模板的对象。
  6. [options.interpolate=_.templateSettings.interpolate] (RegExp): “插值”分隔符。
  7. [options.sourceURL='lodash.templateSources[n]'] (string): 已编译模板的 sourceURL。
  8. [options.variable='obj'] (string): 数据对象变量名。

返回

(Function): 返回编译后的模板函数。

示例

js
// 使用“interpolate”分隔符创建已编译的模板。
const compiled = _.template('hello <%= user %>!')
compiled({ user: 'fred' })
// => 'hello fred!'

// 使用 HTML“escape”分隔符转义数据属性值。
const compiled = _.template('<b><%- value %></b>')
compiled({ value: '<script>' })
// => '<b>&lt;script&gt;</b>'

// 使用“evaluate”分隔符执行 JavaScript 并生成 HTML。
const compiled = _.template('<% _.forEach(users, function(user) { %><li><%- user %></li><% }); %>')
compiled({ users: ['fred', 'barney'] })
// => '<li>fred</li><li>barney</li>'

// 在“evaluate”分隔符中使用内部“print”函数。
const compiled = _.template('<% print("hello " + user); %>!')
compiled({ user: 'barney' })
// => 'hello barney!'

// 使用 ES 模板文字分隔符作为“interpolate”分隔符。
// 通过替换“interpolate”分隔符禁用支持。
const compiled = _.template('hello ${ user }!')
compiled({ user: 'pebbles' })
// => 'hello pebbles!'

// 使用反斜杠将分隔符视为纯文本。
const compiled = _.template('<%= "\\<%- value %\\>" %>')
compiled({ value: 'ignored' })
// => '<%- value %>'

// 使用“imports”选项将“jQuery”导入为“jq”。
const text = '<% jq.each(users, function(user) { %><li><%- user %></li><% }); %>'
const compiled = _.template(text, { imports: { jq: jQuery } })
compiled({ users: ['fred', 'barney'] })
// => '<li>fred</li><li>barney</li>'

// 使用“sourceURL”选项为模板指定自定义 sourceURL。
const compiled = _.template('hello <%= user %>!', { sourceURL: '/basic/greeting.jst' })
compiled(data)
// => 在 Web 检查器的“源”选项卡或“资源”面板下找到“greeting.jst”的源。

// 使用“variable”选项确保未在已编译的模板中使用 with 语句。
const compiled = _.template('hi <%= data.user %>!', { variable: 'data' })
compiled.source
// => function(data) {
//   const __t, __p = '';
//   __p += 'hi ' + ((__t = ( data.user )) == null ? '' : __t) + '!';
//   return __p;
// }

// 使用自定义模板分隔符。
_.templateSettings.interpolate = /\{\{([\s\S]+?)\}\}/g
const compiled = _.template('hello {{ user }}!')
compiled({ user: 'mustache' })
// => 'hello mustache!'

// 使用“source”属性内联已编译的模板,以便在错误消息和堆栈跟踪中显示有意义的行号。
fs.writeFileSync(path.join(process.cwd(), 'jst.js'), `\
  var JST = {\
    "main": ${_.template(mainText).source}\
  };\
`)

_.toLower

string 作为一个整体转换为小写,就像 String#toLowerCase

引入版本 4.0.0

source / npm package

参数

  1. [string=''] (string): 要转换的字符串。

返回

(string): 返回小写字符串。

示例

js
_.toLower('--Foo-Bar--')
_.toLower('fooBar')
_.toLower('__FOO_BAR__')

_.toUpper

string 整体转换为大写,就像 String#toUpperCase

引入版本 4.0.0

source / npm package

参数

  1. [string=''] (string): 要转换的字符串。

返回

(string): 返回大写字符串。

示例

js
_.toUpper('--foo-bar--')
_.toUpper('fooBar')
_.toUpper('__foo_bar__')

_.trim

string 中删除前导和尾随空格或指定字符。

引入版本 3.0.0

source / npm package

参数 ([string=''], [chars=whitespace])

  1. [string=''] (string): 要修剪的字符串。
  2. [chars=whitespace] (string): 要修剪的字符。

返回

(string): 返回修剪后的字符串。

示例

js
_.trim('  abc  ')
_.trim('-_-abc-_-', '_-')
_.map(['  foo  ', '  bar  '], _.trim)

_.trimEnd

string 中删除尾随空格或指定字符。

引入版本 4.0.0

source / npm package

参数 ([string=''], [chars=whitespace])

  1. [string=''] (string): 要修剪的字符串。
  2. [chars=whitespace] (string): 要修剪的字符。

返回

(string): 返回修剪后的字符串。

示例

js
_.trimEnd('  abc  ')
_.trimEnd('-_-abc-_-', '_-')

_.trimStart

string 中删除前导空格或指定字符。

引入版本 4.0.0

source / npm package

参数 ([string=''], [chars=whitespace])

  1. [string=''] (string): 要修剪的字符串。
  2. [chars=whitespace] (string): 要修剪的字符。

返回

(string): 返回修剪后的字符串。

示例

js
_.trimStart('  abc  ')
_.trimStart('-_-abc-_-', '_-')

_.truncate

如果字符串长度超过给定的最大字符串长度,则截断它。 被截断的字符串的最后一个字符被替换为默认为“...”的省略字符串。

引入版本 4.0.0

source / npm package

参数 ([string=''], [options={}])

  1. [string=''] (string): 要截断的字符串。
  2. [options={}] (Object): 选项对象。
  3. [options.length=30] (number): 最大字符串长度。
  4. [options.omission='...'] (string): 省略了指示文本的字符串。
  5. [options.separator] (RegExp|string): 要截断的分隔符模式。

返回

(string): 返回截断的字符串。

示例

js
_.truncate('hi-diddly-ho there, neighborino')
_.truncate('hi-diddly-ho there, neighborino', { length: 24, separator: ' ' })
_.truncate('hi-diddly-ho there, neighborino', { length: 24, separator: /,? +/ })
_.truncate('hi-diddly-ho there, neighborino', { omission: ' [...]' })

_.unescape

_.escape的逆; 此方法将 string 中的 HTML 实体 &amp;&lt;&gt;&quot;&#39; 转换为其对应的字符。

注意: 没有其他 HTML 实体未转义。 要取消转义其他 HTML 实体,请使用第三方库,如 he

引入版本 0.6.0

source / npm package

参数

  1. [string=''] (string): 要取消转义的字符串。

返回

(string): 返回未转义的字符串。

示例

js
_.unescape('fred, barney, &amp; pebbles')

_.upperCase

将作为空格分隔的单词的“字符串”转换为大写。

引入版本 4.0.0

source / npm package

参数

  1. [string=''] (string): 要转换的字符串。

返回

(string): 返回大写字符串。

示例

js
_.upperCase('--foo-bar')
_.upperCase('fooBar')
_.upperCase('__foo_bar__')

_.upperFirst

string 的第一个字符转换为大写。

引入版本 4.0.0

source / npm package

参数

  1. [string=''] (string): 要转换的字符串。

返回

(string): 返回转换后的字符串。

示例

js
_.upperFirst('fred')
_.upperFirst('FRED')

_.words

string 拆分为单词数组。

引入版本 3.0.0

source / npm package

参数 ([string=''], [pattern])

  1. [string=''] (string): 要检查的字符串。
  2. [pattern] (RegExp|string): The pattern to match words.

返回

(Array): 返回 string 的单词。

示例

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