Lodash教程--(1)字符串String

这篇Lodash教程聚焦于字符串String,介绍了一系列方法,如_.camelCase, _.capitalize和_.endsWith等,用于字符串的格式化、转换和操作。文章详细解释了每个方法的用法和规则,帮助开发者提升JavaScript开发效率。" 113489238,10296786,点云配准:刚体变换与旋转平移,"['点云处理', '计算机视觉', '数学模型', 'matlab编程']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

欢迎来到Lodash教程–字符串String

这是lodash系列的第一篇教程,建议学习之前,先复习上一篇《JavaScript教程–标准库系列(1)–String对象》
由于在企业级开发中,我们需要满足快速开发和协作式开发的需求,因此基本上使用lodash而不是直接使用原生的JavaScript。
我们在学习的时候也是先看原生的JavaScript夯实基础,再用lodash巩固并更新知识点。

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

  • [string=’’] (string): 要转换的字符串。
  • 可以是先声明然后在参数位置写字符串名称,也可以直接写字符串。
  • 转换规则实际是消除空格,将输入的字符串的第一个大写字母改成小写,若输入字符串有空格,将空格后的第一个字母改为大写。
  • 返回的内容是参数的驼峰写法。
_.camelCase('Hello World'); // 'helloWorld'

var s1 = 'Hello World';
_.camelCase(s1); //'helloWorld'

_.camelCase('HelloWorld'); // 'helloWorld'
_.camelCase(‘HHi'); //'hHi'

_.camelCase('Just a tEst'); // 'justATEst'

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

  • [string=''](string)输入的字符串
  • 返回结果是首字母大写,其余部分小写。
  • 原字符串的空格保留。
_.capitalize('MONKEY'); // 'Monkey'
_.capitalize('peppa Pig'); // 'Peppa pig'

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

  • [string=''] (string):输入的字符串。
  • [target] (string):指定字符。
  • [position=string.length] (number):指定位置下标。
  • 返回的是一个布尔值(boolean),字符串在指定位置下标的字符是否是指定字符。如果是则返回true,否则返回false
  • 默认的指定位置下标为字符串结尾,因此最后一个参数可以省略。
  • 指定字符也可以是空格。
_.endsWith('good night','t'); //true
_.endsWith('good night','',4);  //true

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

  • 转义string中的 "&", "<", ">", '"', "'", 和 ""` 字符为HTML实体字符。
_.escape('peppa />" pig'); // "peppa /&gt;&quot; pig"
_.escape('<html>'); // "&lt;html&gt;"

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

  • 转换字符串string为短横线命名规则。
  • 将大写字母全部改为小写。
  • 在每个小写变大写的字符前面加短横线。
  • 如果有其他分隔符号,则将其他分隔符号改为短横线。
_.kebabCase('pePpaPig'); //"pe-ppa-pig"
_.kebabCase('__PePPA|Pig__'); // "pe-ppa-pig"
_.kebabCase('____PepPA-PiG'); //"pep-pa-pi-g"

(6) _.lowerCase([string=’’])

  • 转换字符串string以空格分开单词,并转换为小写。
  • 小写变大写的字符前加空格。
  • 删除字符串首尾的空格及符号。
  • 字符串中间的符号用空格代替。
_.lowerCase('--PePpa-pIG'); //"pe ppa p ig"

(7)_.lowerFirst([string=’’])

  • 转换字符串string的首字母为小写。
  • 如果第一个字符不是字母(空格或其他字符)则不改变。
_.lowerFirst('Peppa Pig'); // "peppa Pig"
_.lowerFirst(' Peppa Pig'); // " peppa Pig"
_.lowerFirst('-Peppa Pig'); // "-peppa Pig"

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

  • 如果string字符串长度小于 length 则从左侧和右侧填充字符。 如果没法平均分配,则截断超出的长度。
  • 字符部分若为空,则以空格填充。
  • 若字符串长度大于length,则返回字符串。
_.pad('abc', 9, '_-');//"_-_abc_-_"
_.pad('abc', 6, '_-');//"_abc_-"
_.pad('abc', 2, '_-');//"abc"

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

  • 如果string字符串长度小于 length 则在右侧填充字符。
  • 如果超出length长度则返回原字符串。
  • 若无字符参数则以空格填充。
_.padEnd('hellokitty', 15);//"hellokitty     "
_.padEnd('hellokitty', 3);//"hellokitty"
_.padEnd('hellokitty', 15,'^');//"hellokitty^^^^^"

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

  • 如果string字符串长度小于 length 则在左侧填充字符。
  • 如果超出length长度则返回原字符串。
  • 若无字符参数则以空格填充。
_.padStart('hellokitty', 6);//"hellokitty"
_.padStart('hellokitty', 15,'@');//"@@@@@hellokitty"
_.padStart('hellokitty', 15);//"     hellokitty"

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

  • string字符串为指定基数的整数。 如果基数是 undefined 或者 0,则radix基数默认是10,如果string字符串是16进制,则radix基数为 16
  • 如果string部分非数字组成的字符串,则返回NaN
  • radix部分为负数,则返回NaN
_.parseInt('15');//15
_.parseInt('15',16);//21
_.parseInt('A',16);//10
_.parseInt('15',-2);//NaN
_.parseInt('@');//NaN
_.parseInt('hello');//NaN
_.parseInt('15',8);//13
_.parseInt('15',7);//12

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

  • 重复 n 次给定字符串。
  • 若省略参数n,返回原字符串。
  • 若参数n0或负数,则返回空字符串。
_.repeat('*', 20);//"********************"
_.repeat('@', 0);//""
_.repeat('#', -1);//""

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

  • 替换string字符串中匹配的pattern为给定的replacement
  • pattern在原字符串中不存在,则返回原字符串。
  • 若省略replacement参数,则返回原字符串。
  • 若同时省略patternreplacement参数,则返回原字符串。
_.replace('HelloKitty','Kitty','Siri');//"HelloSiri"
_.replace('HelloKitty','kitty','Siri');//"HelloKitty"
_.replace('HelloKitty','Kitty');//"HelloKitty"
_.replace('HelloKitty')//"HelloKitty"

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

  • 转换字符串stringsnake case.
  • snake case即所有字母小写,单词用下划线隔开。
_.snakeCase('Peppa Pig');//"peppa_pig"

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

  • 根据separator 拆分字符串string
  • [limit] (number): 限制返回结果的数量。
  • separator在原字符串中不存在,则返回原字符串。
  • 若省略separator,则返回原字符串。
_.split('a|b|c', '-', 2);//["a|b|c"]
_.split('a-b-c', 2);//["a-b-c"]
_.split('a-b-c', '', 2);//["a", "-"]

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

  • 转换 string 字符串为 start case.
  • start case:每个单词首字母改为大写,单词间用空格隔开,其余不变。
_.startCase('helloKitty');//"Hello Kitty"
_.startCase('-he-lloKitty');//"He Llo Kitty"
_.startCase('_HELLO_KITTY');//"HELLO KITTY"

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

  • 检查字符串string是否以 target 开头。
  • 若省略position,则默认为第一个字符。
  • position为负数,则自动转为0
  • 若省略target,则返回false
_.startsWith('peppa', 'e',1);//true
_.startsWith('peppa', 'p',-1);//true
_.startsWith('peppa', 'a',-1);//false
_.startsWith('peppa', -6);//false

(18)_.toLower([string=’’])

  • 转换整个string字符串的字符为小写
_.toLower('HELLOKITTY')//"hellokitty"
_.toLower('HELLO KITTY')//"hello kitty"

(19)_.toUpper([string=’’])

  • 转换整个string字符串的字符为大写。
_.toUpper('peppa-pig');//"PEPPA-PIG"

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

  • string字符串中移除前面和后面的空格或指定的字符。
  • 这里面指定的字符是指每一个字符,比如传入的字符为'-|',那么不管这两个字符是否相连,都移除。
  • 默认情况下,若不传入字符参数,则若传入字符参数,并且没有空格,则不删除原字符串的空格。
_.trim('-_|peppa-_-', '_|-');//"peppa"
_.trim('-_|abc-_   -', '_|-');//"abc-_   "

(21)_.trimEnd([string=’’], [chars=whitespace])

  • _.trim([string=''], [chars=whitespace])类似,从string字符串中移除后面的空格指定的字符
_.trimEnd('  peppa  ');//"  peppa"
_.trimEnd('-_-hello-||||||_-', '|_-');//"-_-hello"

(21)_.trimStart([string=’’], [chars=whitespace])

  • _.trim([string=''], [chars=whitespace])类似,从string字符串中移除前面的空格指定的字符
_.trimStart('  peppa  ');//"peppa  "
_.trimStart('-_-hello-||||||_-', '|_-');//"hello-||||||_-"

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

  • 截断string字符串,如果字符串超出了限定的最大值。 被截断的字符串后面会以 omission 代替,omission 默认是 "..."
  • 参数
    [string=''] (string): 要截断的字符串。
    [options={}] (Object): 选项对象。
    [options.length=30] (number): 允许的最大长度。
    [options.omission='...'] (string): 超出后的代替字符。
    [options.separator] (RegExp|string): 截断点。
  • 默认为长度为30,但如果超过了,最后三个用…代替,所以只保留原字符串的27个字符。
_.truncate('Hello Kitty,this is Peppa Pig and Siri',{
'length':20,
'omission':'@@@'});
//"Hello Kitty,this @@@"

_.truncate('Hello Kitty,this is Peppa Pig and Siri',{
'separator':/,/, 
'omission':'@@@'});
//"Hello Kitty@@@"

(23)_.upperCase([string=’’])

  • 转换字符串string为 空格 分隔的大写单词。
  • 具体规则是在小写转大写前面加空格,特殊字符改为空格。
  • 所有的小写字母改为大写。
_.upperCase('helLoKitty');//"HEL LO KITTY"
_.upperCase('helLo-Kitty');//"HEL LO KITTY"
_.upperCase('h-e-lLo-Kitty');//"H E L LO KITTY"

(24)_.upperFirst([string=’’])

  • 转换字符串string的首字母为大写,其余大小写不变。
_.upperFirst('hello kitty');//"Hello kitty"

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

  • 拆分字符串string中的词为数组 。
  • 拆分依据pattern参数。
  • 如果省略了pattern参数,则依据字符串中的符号、空格拆分。
_.words('hello|Kitty|siri');//(3) ["hello", "Kitty", "siri"]
_.words('hello|Kitty|siri',/[^, ]/g);
//(16) ["h", "e", "l", "l", "o", "|", "K", "i", "t", "t", "y", "|", "s", "i", "r", "i"]

注意

本文中省略了_.template([string=''], [options={}]),因为我没看懂?。
鹅且这个方法比较长,等下次需要弄懂这个方法了单独加一篇讲template
_.unescape([string=''])方法同上。
Lodash系列–String部分完结!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值