Skip to content

字符串

clueTrim

String.prototype.clueTrim([needle])

去除首尾字符。

返回值:String

参数说明类型可选值默认值
needle可为空,默认去除空白符。
可指定要去除字符,多个字符使用英文逗号分隔
String
js
console.log(' 123 '.clueTrim());
console.log(' ca123ca '.clueTrim(' ,c,a'));

clueTrimLeft

String.prototype.clueTrimLeft([needle])

去除开头字符。

返回值:String

参数说明类型可选值默认值
needle可为空,默认去除空白符。
可指定要去除字符,多个字符使用英文逗号分隔
String
js
console.log(' 123 '.clueTrimLeft());
console.log(' ca123ca '.clueTrimLeft(' ,c,a'));

clueTrimRight

String.prototype.clueTrimRight([needle])

去除开头字符。

返回值:String

参数说明类型可选值默认值
needle可为空,默认去除空白符。
可指定要去除字符,多个字符使用英文逗号分隔
String
js
console.log(' 123 '.clueTrimRight());
console.log(' ca123ca '.clueTrimRight(' ,c,a'));

toLower

String.prototype.toLower()

转化为小写。

返回值:String

js
console.log('banana'.toLower());

toUpper

String.prototype.toUpper()

转化为大写。

返回值:String

js
console.log('banana'.toUpper());

insert

String.prototype.insert(needle, index)

插入字符串。

返回值:String

参数说明类型可选值默认值
needle要插入的字符串String
index插入的索引位置Number
js
console.log('banan'.insert('a', 5));

indexReplace

String.prototype.indexReplace(needle, index)

索引位置替换。

返回值:String

参数说明类型可选值默认值
needle替换值String
index被替换值的位置Number
js
console.log('banena'.indexReplace('a', 3));

rangeIndexReplace

String.prototype.rangeIndexReplace(needle, startIndex, endIndex)

索引范围替换。

返回值:String

参数说明类型可选值默认值
needle替换值String
startIndex开始替换的索引位置Number
endIndex结束替换的索引位置Number
js
console.log('zxcvbnm'.rangeIndexReplace('abc', 3, 4));

getShowCount

String.prototype.getShowCount(needle)

获取指定字符串出现次数。

返回值:Number

参数说明类型可选值默认值
needle待统计字符串String
js
console.log('banana'.getShowCount('c'));

getShowCounts

String.prototype.getShowCounts()

统计字符串中所有字符的出现次数。

返回值:Object

js
console.log('banana'.getShowCounts());

batchReplace

String.prototype.batchReplace(needle)

字符串批量替换。

参数说明类型可选值默认值
needle映射对象Object

返回值:String

js
var str1 = 'C = (a + b) * 2';
var str2 = str1.batchReplace({
    a: 10,
    b: 5,
});
console.log(eval(str2));

sprintf

String.prototype.sprintf([...])

将传入的萝卜(形参)放到对应的坑位(%s)上。

参数说明类型可选值默认值
arguments萝卜String

返回值:String

js
console.log('Welcome back %s, This is a %s moment.'.sprintf('Jayce', 'wonderful'));