JavaScript中使用ASCII码进行字符串大小写转换
2022-10-20 本文已影响0人
Ringo_
字符转ASCII码:str='a'; str.charCodeAt()
ASCII码转字符:String.fromCharCode(num)
// 将字符串中的大写转为小写
function toLowerCase(s: string): string {
let res = "";
for (let i = 0; i < s.length; i++) {
let c = s.charCodeAt(i);
if (c >= 65 && c <= 90) {
res += String.fromCharCode(c + 32);
} else {
res += s[i];
}
}
return res;
}
toLowerCase("azAZ"); //"azaz"