class Solution {
public String toLowerCase(String str) {
if (str.isEmpty()) {
return null;
}
byte[] num = str.getBytes();
for (int i = 0; i < num.length; i++) {
if (65 <= num[i] && num[i] <= 90) {
num[i] += 32;
}
}
return new String(num);
}
}
解题思路:
- 创建byte[ ] num,将str转换成byte[ ]
- 根据num的长度遍历里面的内容
- 小写字符a-z的byte范围为65-90,而其对应的大写字母为小额字母byte数值+32,以此为依据,替换num中的byte数值
- 返回new String(),将num的值放入。