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"
大小写转换的题,很简单
Python解法
class Solution:
def toLowerCase(self, str: str) -> str:
return str.lower()
Java代码如下:
class Solution {
public String toLowerCase(String str) {
return str.toLowerCase();
}
}
函数说明:
public String toLowerCase()
Converts all of the characters in this String to lower case using the rules of the default locale. This is equivalent to calling toLowerCase(Locale.getDefault()).
Note: This method is locale sensitive, and may produce unexpected results if used for strings that are intended to be interpreted locale independently. Examples are programming language identifiers, protocol keys, and HTML tags. For instance, "TITLE".toLowerCase() in a Turkish locale returns "t\u0131tle", where '\u0131' is the LATIN SMALL LETTER DOTLESS I character. To obtain correct results for locale insensitive strings, use toLowerCase(Locale.ENGLISH).
Returns:
the String, converted to lowercase.
See Also:
toLowerCase(Locale)
Java的正规解法:
class Solution {
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);
}
}