字符串方法
split(分隔符,[limit]) 将字符串拆成数组,超出可选项limit的长度不返回
charAt(index),获得指定索引的字符
indexOf() ,查找字符,没有返回-1 不能查NaN和空位 includes可以
lastIndexOf()
includes(searchValue,start)
startsWith(str)检测是否以指定字符串开始
endsWith()
slice() [start,end)
substr(start,length)
phone.substr(0,3) + ‘****’ + phone.substr(7)
substring(from,to)
toUpperCase() toLowerCase()
replace(searchValue,newValue) 默认只替换第一个,如果要全部替换用正则 /替换内容/g
let str = 'ca-de-ea';
//中划线改小驼峰
let s = str.replace(/-[a-z]/g, (match)=>match.replace('-', '').toUpperCase());
match() 返回匹配的数组
search(searchValue) 返回第一个与正则相匹配的字符串位置索引
padStart(长度,字符)/padEnd() 头部/尾部补全
注意
indexOf没找到则返回值为-1,但不能用if(str.indexOf(st)){}来进行判断
if(-1){
console.log(12)//会输出
}
因为console.log(-1==false) 为false
补齐两位小数
//补齐两位小数
export const returnFloat = (num) => {
num = num.toString() // 转成字符串类型 如能确定类型 这步可省去
if (num.indexOf('.') !== -1) {
let newVal = num.split('.')
if (newVal[1].length === 1) {
newVal = newVal.join('.')
newVal += '0'
num = newVal
}
} else {
num += '.00'
}
return num
}