使用ASCII处理字符串
ASCII码:
[a-z] 97-122
[A-Z] 65-90
空格 32
换行 10
String.charCodeAt() //获取字符的ASCII码
toggle() {
let newMsg = "";
for (let i = 0; i < this.msg.length; i++) {
let letter = this.msg[i];
let asciiCode = letter.charCodeAt();
if ((asciiCode >= 65 && asciiCode <= 90) || (asciiCode >= 97 && asciiCode <= 122) || asciiCode === 32) {
newMsg += letter;
}
}
console.log(newMsg);
}
String.fromCharCode(ASCIICode) //ASCII转字符
(将大写字母的 ASCII 码数值加上 32 便是其对应小写字母的 ASCII 码数值)
lowLetter() {
let newMsg = ""
for(let i = 0; i < this.msg.length; i++) {
let letter = this.msg[i]
let asciiCode = letter.charCodeAt();
if(asciiCode >= 65 && asciiCode <= 90) {
newMsg += String.fromCharCode(asciiCode + 32)
}else{
newMsg += letter
}
}
console.log(newMsg)
}
String.toLowerCase() //字母转小写
String.split() //字符分割
words() {
let newMsg = "";
for (let i = 0; i < this.originText.length; i++) {
let letter = this.originText[i]
let asciiCode = letter.charCodeAt()
if((asciiCode >= 65 && asciiCode <= 90) || (asciiCode >= 97 && asciiCode <= 122) || asciiCode === 32) {
newMsg += letter
}
}
let lowLetter = newMsg.toLowerCase()
console.log("toLowerCase:",lowLetter)
let words = lowLetter.split(' ')
console.log("split:",`${words.length}个单词`)
}
数字
Math.pow(base,exponent) //幂运算
base为底数,exponent为幂数
Math.log(x) //以自然对数e为底的x的对数
使用js计算非以e为底的对数结果
function getBaseLog(base,x) {
return Math.log(x) / Math.log()
}
网友评论