JS 理清字符串的用法

2020-03-09  本文已影响0人  行走的蛋白质

方法

String.fromCharCode()
let str1 = String.fromCharCode(65, 66, 67)
console.log(str1) // ABC
String.prototype.charAt()
let str2 = 'protein'
console.log(str1.charAt(3)) // t
console.log(str1[3]) // t
String.prototype.charCodeAt()
console.log('ABC'.charCodeAt(0)) // 65
console.log('ABC'.charCodeAt(10)) // NaN
console.log('ABC'.charCodeAt(-1)) // NaN
String.prototype.codePointAt()
String.prototype.concat()
let str3 = 'protein'
let str4 = str3.concat('banana', 'apple')
console.log(str4) // proteinbananaapple
String.prototype.includes()
let str5 = 'protein'
console.log(str5.includes('te')) // true
console.log(str5.includes('te', 1)) // true
console.log(str5.includes('te', 10)) // false
String.prototype.search()
let str51 = 'protein2020'
let reg51 = /\d+/
console.log(str51.search(reg51)) // 7
console.log(str51.search('2020')) // 7
String.prototype.endsWith()
let str6 = 'protein tomato potato'
console.log(str6.endsWith('potato')) // true
console.log(str6.endsWith('tomato')) // false
console.log(str6.endsWith('tomato', 14)) // true
String.prototype.startsWith()
String.prototype.indexOf()
let str7 = 'protein tomato potato'
console.log(str7.indexOf('tomato')) // 8
console.log(str7.indexOf('tomato', 0)) // 8
console.log(str7.indexOf('tomato', 10)) // -1
console.log(str7.indexOf('tomato', 100)) // -1

// 统计一个字符串中某个字母出现的次数
let str8 = 'ptoteintomatopotatobanana'
let count = 0
let someString = str8.indexOf('o')
while(someString !== -1) {
    count++
    someString = str8.indexOf('o', someString + 1)
}
console.log(count) // 5
String.prototype.lastIndexOf()
let str9 = 'protein tomato potato'
console.log(str9.lastIndexOf('potato')) // 15
console.log(str9.lastIndexOf('potato', 100)) // 15
console.log(str9.lastIndexOf('potato', 10)) // -1
String.prototype.localeCompare()
let str10 = 'protein'
let str101 = 'potato'
console.log(str10.localeCompare(str101)) // 1
String.prototype.match()
let str11 = 'protein2019bear2020'
let reg11 = /\d+/
console.log(str11.match(reg11)) // [ '2019', index: 7, input: 'protein2019bear2020', groups: undefined ]
reg11 = /\d+/g
console.log(str11.match(reg11)) // [ '2019', '2020' ]
String.prototype.normalize()
let str12 = '\u0066\u0066'
console.log(str12.normalize()) // ff
String.prototype.repeat()
let str13 = 'protein'
// console.log(str13.repeat(-1)) // RangeError: repeat count must be positive and less than inifinity
console.log(str13.repeat(0)) // ''
console.log(str13.repeat(1)) // protein
console.log(str13.repeat(2)) // proteinprotein
String.prototype.replace()
let str14 = 'protein is a protein'
let str141 = str14.replace('protein', 'potato')
console.log(str141) // potato is a protein
console.log(str14) // protein is a protein
let str142 = str141.replace(/\bp[a-z]*n\b/, 'potato')
console.log(str142) // potato is a potato
let str143 = 'protein potato'
let str144 = str143.replace(/(\w+)\s(\w+)/, '$2 $1')
console.log(str144) // potato protein 
String.prototype.slice()
let str15 = 'protein'
let str151 = str15.slice(1, 1)
let str152 = str15.slice(1, 2)
let str153 = str15.slice(1, 10)
console.log(str15) // protein
console.log(str151) // ''
console.log(str152) // r
console.log(str153) // rotein
String.prototype.split()
let str16 = 'protein,jfla'
let arr16 = str16.split(',')
let arr161 = str16.split(',', 1)
console.log(arr16) // [ 'protein', 'jfla' ]
console.log(arr161) // [ 'protein' ]
console.log(arr16.join()) // protein,jfla
String.prototype.toLowerCase()
String.prototype.toUpperCase()
String.prototype.substr()
let str17 = 'protein'
console.log(str17.slice(1, 2)) // r
console.log(str17.substr(1, 2)) // ro
console.log(str17.substr(-6, 2)) // ro
String.prototype.substring()
let str18 = 'protein'
console.log(str18.slice(1, 2))
console.log(str18.substring(1, 2))
上一篇下一篇

猜你喜欢

热点阅读