尝试以下尺寸:
String properCase (String inputVal) { // Empty strings should be returned as-is. if (inputVal.length() == 0) return ""; // Strings with only one character uppercased. if (inputVal.length() == 1) return inputVal.toUpperCase(); // Otherwise uppercase first letter, lowercase the rest. return inputVal.substring(0,1).toUpperCase() + inputVal.substring(1).toLowerCase();}基本上,它首先处理空字符串和一个字符字符串的特殊情况,否则正确处理一个两字符以上的字符串。而且,正如评论中指出的那样,功能不需要使用一个字符的特殊情况,但我仍然希望明确,特别是如果它导致更少的无用调用(例如子字符串以获取空字符串),小写字母它,然后附加它。



