String 对象 js, lodash

js: https://www.runoob.com/jsref/jsref-obj-string.html

属性:
constructor 对创建该对象的函数的引用
length 字符串的长度
prototype 允许您向对象添加属性和方法
方法:str.func()
charAt(idx) 返回在指定位置的字符。
charCodeAt(idx) 返回在指定的位置的字符的 Unicode 编码。
concat(str1,str2...) 连接两个或更多字符串,并返回新的字符串。
endsWith(str1) 判断当前字符串是否是以指定的子字符串结尾的(区分大小写)。
String.fromCharCode(num) 将 Unicode 编码转为字符。
indexOf(str1) 返回某个指定的字符串值在字符串中首次出现的位置。
includes(str) 查找字符串中是否包含指定的子字符串。
lastIndexOf(str) 从后向前搜索字符串,并从起始位置(0)开始计算返回字符串最后出现的位置。
match(reg) 查找找到一个或多个正则表达式的匹配。
repeat(num) 复制字符串指定次数,并将它们连接在一起返回。
replace(oldstr,new) 在字符串中查找匹配的子串,并替换与正则表达式匹配的子串。
replaceAll(oldstr,new) 在字符串中查找匹配的子串,并替换与正则表达式匹配的所有子串。
search(str) 查找与正则表达式相匹配的值。
slice(start,end) 提取字符串的片断,并在新的字符串中返回被提取的部分。
split(separator) 把字符串分割为字符串数组。
startsWith(str1) 查看字符串是否以指定的子字符串开头。
substr(start,len) 从起始索引号提取字符串中指定数目的字符。
substring(start,end) 提取字符串中两个指定的索引号之间的字符。包含开始不包含结束
toLowerCase() 把字符串转换为小写。
toUpperCase() 把字符串转换为大写。
trim() 去除字符串两边的空白。
toLocaleLowerCase() 根据本地主机的语言环境把字符串转换为小写。
toLocaleUpperCase() 根据本地主机的语言环境把字符串转换为大写。
valueOf() 返回某个字符串对象的原始值。
toString() 返回一个字符串。

lodash

https://lodash.net/docs/4.16.1.html#_camelcasestring

camelCase(str) // 返回驼峰写法的字符串。

let str = 'hello__world_HelloWorld_hello'
console.log(str,camelCase(str))
// hello_world_HelloWorld_hello helloWorldHelloWorldHello //去掉所有下划线

capitalize(str) // 转换字符串string首字母为大写,剩下为小写。

let str = 'hello__world_HelloWorld_hello'
console.log(str,capitalize(str))
// hello__world_HelloWorld_hello Hello__world_helloworld_hello //不去掉下划线

endsWith(str, str1, [position=string.length]) //检查字符串string是否以给定的target字符串结尾。
escape(str) // 转义string中的 "&", "<", ">", '"', "'", 和 "`" 字符为HTML实体字符。
escapeRegExp(str) // 转义 RegExp 字符串中特殊的字符 "^", "$", "", ".", "*", "+", "?", "(", ")", "[", "]", "{", "}", 和 "|" in .

escapeRegExp('[lodash](https://lodash.com/)');
// => '\[lodash\]\(https://lodash\.com/\)'

kebabCase(str) //将字符串转为中横线连接,下划线,空格,大写都转换为中横线

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

lowerCase(str) // 大写,中横线,下划线转换为空格
lowerFirst(str) // 首字母小写
pad(str, len=0, val=' ') //如果string字符串长度小于 length 则从左侧和右侧填充字符。 如果没法平均分配,则截断超出的长度。默认填充空格,两侧填充

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

padEnd(str, len=0, val=' ') // 如果string字符串长度小于 length 则在右侧填充字符。 如果超出length长度则截断超出的部分。只在右侧填充
padStart(str, len=0, val=' ') // 左侧填充
repeat(str,num) // 重复字符串
replace(str, pattern, replacement) // 字符串替换,pattern 可以是正则或字符串,
snakeCase(str) // 空格,中横线转为下划线,所有大写转小写,去掉首尾连字符

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

split(str, separator, [limit]) // 字符串转数组
startCase(str) // 连接符转空格,去掉首尾连字符,转为大写开头的字母,如果全部是大写,那就不转
startsWith(str, val, [position=0]) // 检查字符串string是否以 target 开头。

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

template(str, [options={}]) // 创建一个预编译模板方法 : https://lodash.net/docs/4.16.1.html#_templatestring-options
toLower(str) // 字符串转小写,连接符不变
toUpper(str) // 转大写
trim(str, [chars=whitespace]) // 去掉两侧空格或指定字符

trim('-_-abc-_-', '_-');
// => 'abc'

trimEnd(str, [chars=whitespace])
trimStart(str, [chars=whitespace])
truncate(str, [options={}]) // 截断string字符串,如果字符串超出了限定的最大值。 被截断的字符串后面会以 omission 代替,omission 默认是 "..."。 : https://lodash.net/docs/4.16.1.html#_truncatestring-options

truncate('hi-diddly-ho there, neighborino', {
'length': 24,
'separator': ' '
});
// => 'hi-diddly-ho there,...'

unescape(str) // escape反向,转换string字符串中的 HTML 实体 &amp;, &lt;, &gt;, &quot;, &#39;, 和 &#96; 为对应的字符。
upperCase(str) // 转换为空格分隔的大写单词
upperFirst(str) // 首字母大写
words(str, [pattern]) // 拆分字符串string中的词为数组 。

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

 

posted @ 2022-09-08 12:15  jqynr  阅读(152)  评论(0)    收藏  举报