String
数组转字符串
数组转字符串
js
const fruits = ['Banana', 'Orange', 'Apple', 'Mango']
const energy = fruits.join()
console.log(energy) // Banana,Orange,Apple,Mango
const fruits = ['Banana', 'Orange', 'Apple', 'Mango']
const energy = fruits.join()
console.log(energy) // Banana,Orange,Apple,Mango
字符串转数组
js
Array.from('foo') // [ "f", "o", "o" ]
Array.from('foo') // [ "f", "o", "o" ]
字符串转换数字
parseInt() 函数解析字符串并返回整数
js
const a = '111'
console.log(typeof parseInt(a)) // number
const a = '111'
console.log(typeof parseInt(a)) // number
强制类型转换
js
const a = '111' * 1
console.log(typeof a) // number
const a = +'111'
console.log(typeof a) // number
const a = '111' * 1
console.log(typeof a) // number
const a = +'111'
console.log(typeof a) // number
Number
js
const a = '1111'
console.log(Number(a))
const a = '1111'
console.log(Number(a))
获取指定字符出现的所有位置
循环使用indexOf,并存储index值
js
const stringValue = "Lorem ipsum dolor sit amet, consectetur adipisicing elit"
function getAllIndex (str,char) {
const positions = []
let index = str.indexOf(char)
while (index > -1) {
positions.push(index)
index = str.indexOf(char, index + 1)
}
return positions
}
console.log(getAllIndex(stringValue,"e")) // "3,24,32,35,52"
const stringValue = "Lorem ipsum dolor sit amet, consectetur adipisicing elit"
function getAllIndex (str,char) {
const positions = []
let index = str.indexOf(char)
while (index > -1) {
positions.push(index)
index = str.indexOf(char, index + 1)
}
return positions
}
console.log(getAllIndex(stringValue,"e")) // "3,24,32,35,52"
去除字符串中指定文本
不影响原字符串
js
const oldStr = "123系统"
const newStr = oldStr.replace('系统','')
const oldStr = "123系统"
const newStr = oldStr.replace('系统','')
判断是否全是中文
js
function isAllChinese (temp) {
return !/[^\u4e00-\u9fa5]/.test(temp)
}
function isAllChinese (temp) {
return !/[^\u4e00-\u9fa5]/.test(temp)
}
转驼峰字符串
js
const result = res.data.trans_result[0].dst.split(" ")
result.forEach((item, index) => {
result[index] = item.slice(0, 1).toUpperCase() + item.slice(1)
})
result[0] = result[0].toLowerCase()
const result = res.data.trans_result[0].dst.split(" ")
result.forEach((item, index) => {
result[index] = item.slice(0, 1).toUpperCase() + item.slice(1)
})
result[0] = result[0].toLowerCase()
驼峰转串式
js
function toLowerLine(str) {
const temp = str.replace(/[A-Z]/g,(match)=> {
return "-" + match.toLowerCase()
})
if(temp.slice(0,1) === '-'){ //如果首字母是大写,执行replace时会多一个_,这里需要去掉
temp = temp.slice(1)
}
return temp
}
console.log(toLowerLine("helloWorld"))
function toLowerLine(str) {
const temp = str.replace(/[A-Z]/g,(match)=> {
return "-" + match.toLowerCase()
})
if(temp.slice(0,1) === '-'){ //如果首字母是大写,执行replace时会多一个_,这里需要去掉
temp = temp.slice(1)
}
return temp
}
console.log(toLowerLine("helloWorld"))
插入字符串至指定位置
js
function insertStr(soure, start, newStr) {
return soure.slice(0, start) + newStr + soure.slice(start)
}
function insertStr(soure, start, newStr) {
return soure.slice(0, start) + newStr + soure.slice(start)
}
根据索引替换文本
js
function replaceStr(source,start, end, str) {
return source.slice(0, start) + str + source.slice(end)
}
const str = '123456'
console.log(replaceStr(str , 2, 4, 'test'));
function replaceStr(source,start, end, str) {
return source.slice(0, start) + str + source.slice(end)
}
const str = '123456'
console.log(replaceStr(str , 2, 4, 'test'));