709. To Lower Case

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"

class Solution {
    public String toLowerCase(String str) {
        int k = (int) ('a');
        StringBuilder sb = new StringBuilder();
        for(char c: str.toCharArray()){
            int t = (int) (c);
            if(!Character.isLetter(c)) sb.append(c);
            else if(t >= k) sb.append(c);
            else sb.append((char) (t + 32));
        }
        return sb.toString();
    }
}
    public String toLowerCase(String str) {
        char[] a = str.toCharArray();
        for (int i = 0; i < a.length; i++)
            if ('A' <= a[i] && a[i] <= 'Z')
                a[i] = (char) (a[i] - 'A' + 'a');
        return new String(a);
    }
原文地址:https://www.cnblogs.com/wentiliangkaihua/p/12898942.html