给你一个字符串
s
,将该字符串中的大写字母转换成相同的小写字母,返回新的字符串。
/**
* @param {string} s
* @return {string}
*/
var toLowerCase = function (s) {
let str = "";
for (let c of s) {
// A - Z => 65 - 90
str +=
c.charCodeAt() >= 65 && c.charCodeAt() <= 90
? String.fromCharCode(c.charCodeAt() | 32)
: c;
}
return str;
};
var toLowerCase = function (s) {
return Array.from(s)
.map((c) =>
c.charCodeAt() >= 65 && c.charCodeAt() <= 90
? String.fromCharCode(c.charCodeAt() + 32)
: c
)
.join("");
};
var toLowerCase = function (s) {
return s.replace(/[A-Z]/g, (c) => String.fromCharCode(c.charCodeAt() + 32));
};
大写变小写、小写变大写 : 字符 ^= 32;
大写变小写、小写变小写 : 字符 |= 32;
小写变大写、大写变大写 : 字符 &= -33;
ASCII码表中大写的A是65,小写的a是97,它们的差是32
65 | 32 转为二进制(按8位来算)可以得到 0100 0001 | 0010 0000 = 0110 0001 = 97 = a