Module: string
字符相关函数
Functions
camelCase
▸ camelCase(str
): string
返回驼峰风格的字符串
Example
//'aBC'console.log(_.camelCase('a-b c'))//mixCase//'loveLovesToLoveLove'console.log(_.camelCase('Love loves to love Love'))//spaces//'aBC'console.log(_.camelCase('a B-c'))//camelCase//'getMyUrl'console.log(_.camelCase('getMyURL'))//camelCase
Parameters
Name | Type |
---|---|
str | string |
Returns
string
返回新字符串
capitalize
▸ capitalize(str
): string
把字符串的首字母大写,如果首字母不是ascii中的a-z则返回原值
Example
//Abcconsole.log(_.capitalize('abc'))//''console.log(_.capitalize(null))//1console.log(_.capitalize(1))
Parameters
Name | Type | Description |
---|---|---|
str | string | 字符串 |
Returns
string
对于null/undefined会返回空字符串
endsWith
▸ endsWith(str
, searchStr
, position?
): boolean
验证字符串是否以查询子字符串结尾
Example
//trueconsole.log(_.endsWith('func.js','js'))//trueconsole.log(_.endsWith('func.js','c',4))
Parameters
Name | Type | Description |
---|---|---|
str | string | |
searchStr | string | 查询字符串 |
position? | number | 索引 |
Returns
boolean
如果以查询子字符串开头返回true,否则返回false
escapeRegExp
▸ escapeRegExp(str
): string
转义正则字符串中的特殊字符,包括 '', '$', '(', ')', '*', '+', '.', '', '', '?', '^', '{', '}', '|'
Example
//\^\[func\.js\] \+ {crud-vue} = \.\*\?\$console.log(_.escapeRegExp('^[func.js] + {crud-vue} = .*?$'))
Since
1.3.0
Parameters
Name | Type | Description |
---|---|---|
str | string | 需要转义的字符串 |
Returns
string
转义后的新字符串
indexOf
▸ indexOf(str
, search
, fromIndex?
): number
查找指定值在字符串中首次出现的位置索引
Example
//10console.log(_.indexOf('cyberfunc.js','js'))//10console.log(_.indexOf('cyberfunc.js','js',5))
Parameters
Name | Type | Description |
---|---|---|
str | string | |
search | string | 指定字符串 |
fromIndex? | number | 起始索引 |
Returns
number
第一个匹配搜索字符串的位置索引或-1
kebabCase
▸ kebabCase(str
): string
返回短横线风格的字符串
Example
//'a-b-c'console.log(_.kebabCase('a_b_c'))//snakeCase//'webkit-perspective-origin-x'console.log(_.kebabCase('webkitPerspectiveOriginX'))//camelCase//'a-b-c'console.log(_.kebabCase('a B-c'))//mixCase//'get-my-url'console.log(_.kebabCase('getMyURL'))//camelCase
Parameters
Name | Type |
---|---|
str | string |
Returns
string
返回新字符串
lastIndexOf
▸ lastIndexOf(str
, search
, fromIndex?
): number
查找指定值在字符串中最后出现的位置索引
Example
//10console.log(_.lastIndexOf('cyberfunc.js','js'))//-1console.log(_.lastIndexOf('cyberfunc.js','js',5))
Parameters
Name | Type | Description |
---|---|---|
str | string | |
search | string | 指定字符串 |
fromIndex? | number | 起始索引,从起始索引位置向左查找指定字符串 |
Returns
number
最后一个匹配搜索字符串的位置索引或-1
lowerCase
▸ lowerCase(str
): string
返回所有字母是小写格式的字符串
Example
//''console.log(_.lowerCase())//'func.js'console.log(_.lowerCase('FUNC.JS'))
Parameters
Name | Type |
---|---|
str | string |
Returns
string
返回新字符串
lowerFirst
▸ lowerFirst(str
): string
转换字符串第一个字符为小写并返回
Example
//'fIRST'console.log(_.lowerFirst('FIRST'))//mixCase//'love loves to love Love'console.log(_.lowerFirst('Love loves to love Love'))//spaces
Parameters
Name | Type |
---|---|
str | string |
Returns
string
返回新字符串
padEnd
▸ padEnd(str
, len
, padString?
): string
使用填充字符串填充原字符串达到指定长度。从原字符串末尾开始填充。
Example
//100console.log(_.padEnd('1',3,'0'))//1-0-0-console.log(_.padEnd('1',6,'-0'))//1console.log(_.padEnd('1',0,'-0'))
Parameters
Name | Type | Description |
---|---|---|
str | string | 原字符串 |
len | number | 填充后的字符串长度,如果长度小于原字符串长度,返回原字符串 |
padString? | string | - |
Returns
string
在原字符串末尾填充至指定长度后的字符串
padStart
▸ padStart(str
, len
, padString?
): string
使用填充字符串填充原字符串达到指定长度。从原字符串起始开始填充。
Example
//001console.log(_.padStart('1',3,'0'))
Parameters
Name | Type | Description |
---|---|---|
str | string | 原字符串。如果非字符串则会自动转换成字符串 |
len | number | 填充后的字符串长度,如果长度小于原字符串长度,返回原字符串 |
padString? | string | - |
Returns
string
在原字符串起始填充至指定长度后的字符串
padZ
▸ padZ(str
, len
): string
使用字符0填充原字符串达到指定长度。从原字符串起始位置开始填充。
Example
//001console.log(_.padZ('1',3))
Parameters
Name | Type | Description |
---|---|---|
str | string | 原字符串 |
len | number | 填充后的字符串长度 |
Returns
string
填充后的字符串
pascalCase
▸ pascalCase(str
): string
返回帕斯卡风格的字符串
Example
//'LoveLovesToLoveLove'console.log(_.pascalCase('Love loves to love Love'))//spaces//'ABC'console.log(_.pascalCase('a B-c'))//mixCase//'GetMyUrl'console.log(_.pascalCase('getMyURL'))//camelCase//'AbCdEf'console.log(_.pascalCase('AB_CD_EF'))//snakeCase//'ABcDEfGhXy'console.log(_.pascalCase('aBc D__EF_GH----XY_'))//mixCase
Parameters
Name | Type |
---|---|
str | string |
Returns
string
返回新字符串
repeat
▸ repeat(str
, count
): string
创建一个以原字符串为模板,重复指定次数的新字符串
Example
//funcfuncfuncconsole.log(_.repeat('func',3))
Parameters
Name | Type | Description |
---|---|---|
str | string | 原字符串 |
count | number | 重复次数 |
Returns
string
对于null/undefined会返回空字符串
replace
▸ replace(str
, searchValue
, replaceValue
): string
使用replaceValue
替换str
中的首个searchValue
部分
Example
//'func-js'console.log(_.replace('func.js','.','-'))//''console.log(_.replace(null,'.','-'))//'kelikeli'console.log(_.replace('geligeli',/ge/g,'ke'))//'geligeli'console.log(_.replace('kelikeli',/ke/g,()=>'ge'))
Parameters
Name | Type | Description |
---|---|---|
str | string | 字符串。非字符串值会自动转换成字符串 |
searchValue | string | RegExp | 查找内容,正则或者字符串 |
replaceValue | string | (substring : string , ...args : any ) => string | 替换内容,字符串或处理函数。函数的返回值将用于替换 |
Returns
string
替换后的新字符串
replaceAll
▸ replaceAll(str
, searchValue
, replaceValue
): string
使用replaceValue
替换str
中的所有searchValue
部分
Example
//'a-b-c'console.log(_.replaceAll('a.b.c','.','-'))//''console.log(_.replaceAll(null,'.','-'))//'kelikeli'console.log(_.replaceAll('geligeli',/ge/,'ke'))//'geligeli'console.log(_.replaceAll('kelikeli',/ke/g,()=>'ge'))
Since
1.3.0
Parameters
Name | Type | Description |
---|---|---|
str | string | 字符串。非字符串值会自动转换成字符串 |
searchValue | string | RegExp | 查找内容,正则或者字符串。非global模式的正则对象会自动转为global模式 |
replaceValue | string | (substring : string , ...args : any ) => string | 替换内容,字符串或处理函数。函数的返回值将用于替换 |
Returns
string
替换后的新字符串
▸ replaceAll(str
, replacement
): string
Parameters
Name | Type |
---|---|
str | string |
replacement | Record <string , any > |
Returns
string
snakeCase
▸ snakeCase(str
): string
返回下划线风格的字符串
Example
//'a_b_c'console.log(_.snakeCase('a-b c'))//mixCase//'love_loves_to_love_love'console.log(_.snakeCase('Love loves to love Love'))//spaces//'a_b_c'console.log(_.snakeCase('a B-c'))//camelCase//'get_my_url'console.log(_.snakeCase('getMyURL'))//camelCase
Parameters
Name | Type |
---|---|
str | string |
Returns
string
返回新字符串
split
▸ split(str
, separator
, limit?
): string
使用分隔符将字符串分割为多段数组
Example
//["func", "js"]console.log(_.split('func.js','.'))//["func"]console.log(_.split('func.js','.',1))
Parameters
Name | Type | Description |
---|---|---|
str | string | 原字符串。如果非字符串则会自动转换成字符串 |
separator | string | RegExp | 分隔符 |
limit? | number | 限制返回的结果数量,为空返回所有结果 |
Returns
string
分割后的数组
startsWith
▸ startsWith(str
, searchStr
, position?
): boolean
验证字符串是否以查询子字符串开头
Example
//trueconsole.log(_.startsWith('func.js','func'))//falseconsole.log(_.startsWith('func.js','func',3))//trueconsole.log(_.startsWith('func.js','c',3))
Parameters
Name | Type | Description |
---|---|---|
str | string | |
searchStr | string | 查询字符串 |
position? | number | 索引 |
Returns
boolean
如果以查询子字符串开头返回true,否则返回false
substring
▸ substring(str
, indexStart?
, indexEnd?
): string
对字符串进行截取,返回从起始索引到结束索引间的新字符串。
Example
//"34567"console.log(_.substring('12345678',2,7))//"345678"console.log(_.substring('12345678',2))//""console.log(_.substring())
Parameters
Name | Type | Description |
---|---|---|
str | string | 需要截取的字符串,如果非字符串对象会进行字符化处理。基本类型会直接转为字符值,对象类型会调用toString()方法 |
indexStart? | number | 起始索引,包含 |
indexEnd? | number | 结束索引,不包含 |
Returns
string
test
▸ test(str
, pattern
, flags?
): boolean
检测字符串是否与指定的正则匹配
Example
//true 忽略大小写包含判断console.log(_.test('func.js','Func','i'))//true 忽略大小写相等判断console.log(_.test('func.js',/^FUNC\.js$/i))//falseconsole.log(_.test('func.js',/FUNC/))
Since
0.19.0
Parameters
Name | Type | Description |
---|---|---|
str | string | |
pattern | string | RegExp | 指定正则。如果非正则类型会自动转换为正则再进行匹配 |
flags? | string | 如果pattern参数不是正则类型,会使用该标记作为正则构造的第二个参数 |
Returns
boolean
匹配返回true
toFixed
▸ toFixed(v
, scale?
): string
截取数字小数位。用来修复原生toFixed函数的bug
Example
//14.05console.log(_.toFixed(14.049,2))//-15console.log(_.toFixed(-14.6))//14.0001console.log(_.toFixed(14.00005,4))//0.101console.log(_.toFixed(0.1009,3))//2.47console.log(_.toFixed(2.465,2))//2.46 原生console.log((2.465).toFixed(2))
Parameters
Name | Type | Description |
---|---|---|
v | string | number | 数字或数字字符串 |
scale? | number | 小数位长度 |
Returns
string
填充后的字符串
toString
▸ toString(v
): string
转换任何对象为字符串。如果对象本身为string类型的值/对象,则返回该对象的字符串形式。否则返回对象的toString()方法的返回值
Example
//''console.log(_.toString(null))//1console.log(_.toString(1))//3,6,9console.log(_.toString([3,6,9]))//-0console.log(_.toString(-0))//[object Set]console.log(_.toString(new Set([3,6,9])))//{a:1}console.log(_.toString({a:1,toString:()=>'{a:1}'}))
Parameters
Name | Type | Description |
---|---|---|
v | any | 任何值 |
Returns
string
对于null/undefined会返回空字符串
trim
▸ trim(str
): string
从字符串的两端删除空白字符。
Example
//holyhighconsole.log(_.trim(' holyhigh '))
Parameters
Name | Type |
---|---|
str | string |
Returns
string
对于null/undefined会返回空字符串
trimEnd
▸ trimEnd(str
): string
从字符串末尾删除空白字符。
Example
//' holyhigh'console.log(_.trimEnd(' holyhigh '))
Parameters
Name | Type |
---|---|
str | string |
Returns
string
对于null/undefined会返回空字符串
trimStart
▸ trimStart(str
): string
从字符串起始位置删除空白字符。
Example
//'holyhigh 'console.log(_.trimStart(' holyhigh '))
Parameters
Name | Type |
---|---|
str | string |
Returns
string
对于null/undefined会返回空字符串
truncate
▸ truncate(str
, len
, options?
): string
对超过指定长度的字符串进行截取并在末尾追加代替字符
Example
//func...console.log(_.truncate('func.js',4))//func...console.log(_.truncate('func.js',6,{separator:/\.\w+/g}))//func.js.com...console.log(_.truncate('func.js.com.cn',13,{separator:'.'}))//func.jsconsole.log(_.truncate('func.js',10))//fun!!!console.log(_.truncate('func.js',3,{omission:'!!!'}))
Since
1.3.0
Parameters
Name | Type | Description |
---|---|---|
str | string | |
len | number | 最大长度。如果长度大于str 长度,直接返回str |
options? | Object | 可选项 |
options.omission? | "..." | 替代字符,默认 '...' |
options.separator? | string | RegExp | 截断符。如果截取后的字符串中包含截断符,则最终只会返回截断符之前的内容 |
Returns
string
返回新字符串
upperCase
▸ upperCase(str
): string
返回所有字母是大写格式的字符串
Example
//''console.log(_.upperCase())//'FUNC.JS'console.log(_.upperCase('func.js'))
Parameters
Name | Type |
---|---|
str | string |
Returns
string
返回新字符串
upperFirst
▸ upperFirst(str
): string
转换字符串第一个字符为大写并返回
Example
//'First'console.log(_.upperFirst('first'))//mixCase//'GetMyURL'console.log(_.upperFirst('getMyURL'))//camelCase
Parameters
Name | Type |
---|---|
str | string |
Returns
string
返回新字符串