用js实现toLowerCase()、toUpperCase()?

怎么像在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


红颜莎娜
浏览 975回答 1
1回答

哈士奇WWW

function UpperCase(s) {&nbsp; &nbsp; var str = s;&nbsp; &nbsp; var arr = str.split("");&nbsp; &nbsp; var charCodeOfA = 'A'.charCodeAt();&nbsp; &nbsp; var charCodeOfZ = 'Z'.charCodeAt();&nbsp; &nbsp; var difference = charCodeOfA - 'a'.charCodeAt();&nbsp; &nbsp; var curChar;&nbsp; &nbsp; for(var i = 0; i < arr.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; curChar = arr[i].charCodeAt();&nbsp; &nbsp; &nbsp; &nbsp; arr[i] = (curChar < charCodeOfZ && curChar > charCodeOfA) ? arr[i] : String.fromCharCode(curChar + difference);&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; str = arr.join("");&nbsp; &nbsp; return str;}var s = "aaaSSF";UpperCase(s); //AAASSF
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript