To Lower Case
Description
Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase.
Example 1:
Input: "Hello"
Output: "hello"
Example 2:
Input: "here"
Output: "here"
Example 3:
Input: "LOVELY"
Output: "lovely"
Discuss
直接遍历就可以
Code
class Solution {
public String toLowerCase(String str) {
if (str == null || str.length() == 0) { return null; }
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c >= 'A' && c <= 'Z') {
c += 32;
sb.append(Character.toString((char)c));
continue;
}
sb.append(c);
}
return sb.toString();
}
}