【LeetCode】709(Java)To Lower Case
阿新 • • 發佈:2019-01-31
Question:
Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase.
實現函式ToLowerCase str()有一個字串引數,並返回相同的字串小寫。
Example 1:
Input: "Hello"
Output: "hello"
Example 2:
Input: "here"
Output: "here"
Example 3:
Input: "LOVELY"
Output: "lovely"
方法一:
public String toLowerCase(String str) {
char[] s = str.toCharArray();
for(int i = 0 ; i < str.length() ; i++) {
if(s[i] >= 'A' && s[i] <= 'Z') {
s[i] += 32;
}
}
return new String(s);
}
方法二:
public String toLowerCase(String str) { StringBuilder sb = new StringBuilder(); for(int i=0 ; i < str.length() ; i++) { char s = str.charAt(i); if(s >= 'A' && s <= 'Z') { s += 32; sb.append(Character.toString((char)s)); continue; } sb.append(s); } return sb.toString(); }