/** * 功能:切换字符串中的所有字母大小写。<br> * * <pre> * StringUtil.swapCase(null) = null * StringUtil.swapCase("") = "" * StringUtil.swapCase("The dog has a BONE") = "tHE DOG HAS A bone" * </pre> * * @param str 源字符串 * @return String */ public static String swapCase(String str) { if (StringUtil.isEmpty(str)) { return str; } char[] buffer = str.toCharArray(); boolean whitespace = true; for (int i = 0; i < buffer.length; i++) { char ch = buffer[i]; if (Character.isUpperCase(ch)) { buffer[i] = Character.toLowerCase(ch); whitespace = false; } else if (Character.isTitleCase(ch)) { buffer[i] = Character.toLowerCase(ch); whitespace = false; } else if (Character.isLowerCase(ch)) { if (whitespace) { buffer[i] = Character.toTitleCase(ch); whitespace = false; } else { buffer[i] = Character.toUpperCase(ch); } } else { whitespace = Character.isWhitespace(ch); } } return new String(buffer); }
/** * 检查是否都是大写. * * <pre> * StringUtil.isAllUpperCase(null) = false * StringUtil.isAllUpperCase("") = false * StringUtil.isAllUpperCase(" ") = false * StringUtil.isAllUpperCase("ABC") = true * StringUtil.isAllUpperCase("aBC") = false * </pre> * * @param cs 源字符串 * @return String */ public static boolean isAllUpperCase(String cs) { if (cs == null || StringUtil.isEmpty(cs)) { return false; } int sz = cs.length(); for (int i = 0; i < sz; i++) { if (Character.isUpperCase(cs.charAt(i)) == false) { return false; } } return true; }