字符串大小写互相转换
1、字符串转换为char[],再进行转换
2、位运算
https://blog.csdn.net/qq_37974048/article/details/102797958
大小或32(|32),小大且-33(&-33),大小小大方32(^32).
实现函数 ToLowerCase(),该函数接收一个字符串参数 str,并将该字符串中的大写字母转换成小写字母,之后返回新的字符串。
示例 1:
输入: “Hello”
输出: “hello”
示例 2:
输入: “here”
输出: “here”
示例 3:
输入: “LOVELY”
输出: “lovely”
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/to-lower-case
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
class Solution {
public String toLowerCase(String str) {
//可记住位运算。
StringBuilder result = new StringBuilder();
for(char c : str.toCharArray()){
//字符数组得方式
// if(c>='A'&&c<='Z'){
// c = (char)(c+32);
// }
//位运算的方式
c |= 32;
result.append(c);
}
return result.toString();
}
}