怎么像在C语言里面通过ASSIC码做转换?实现 string.prototype.toUpperCase()、string.prototype.toLowerCase()function UpperCase(s){ var str = s; var arr = str.split(""); for(var i=0; i<arr.length; i++){
arr[i] = arr[i] + 'a' -'A';
}
str = arr.join(""); return str;
}var s = "aaaSSF";
UpperCase(s); //NANNANNAN
1 回答
哈士奇WWW
TA贡献1799条经验 获得超6个赞
function UpperCase(s) {
var str = s;
var arr = str.split("");
var charCodeOfA = 'A'.charCodeAt();
var charCodeOfZ = 'Z'.charCodeAt();
var difference = charCodeOfA - 'a'.charCodeAt();
var curChar;
for(var i = 0; i < arr.length; i++) {
curChar = arr[i].charCodeAt();
arr[i] = (curChar < charCodeOfZ && curChar > charCodeOfA) ? arr[i] : String.fromCharCode(curChar + difference);
}
str = arr.join("");
return str;
}
var s = "aaaSSF";
UpperCase(s); //AAASSF
添加回答
举报
0/150
提交
取消