Refactor and enhance the 'Upper' class (#6118)

This commit is contained in:
p@ren
2025-01-12 11:29:27 +00:00
committed by GitHub
parent 08c0f4ac2d
commit bd785dea4d

View File

@ -21,15 +21,19 @@ public final class Upper {
* @return the {@code String}, converted to uppercase. * @return the {@code String}, converted to uppercase.
*/ */
public static String toUpperCase(String s) { public static String toUpperCase(String s) {
if (s == null || s.isEmpty()) { if (s == null) {
throw new IllegalArgumentException("Input string connot be null");
}
if (s.isEmpty()) {
return s; return s;
} }
char[] values = s.toCharArray(); StringBuilder result = new StringBuilder(s);
for (int i = 0; i < values.length; ++i) { for (int i = 0; i < result.length(); ++i) {
if (Character.isLetter(values[i]) && Character.isLowerCase(values[i])) { char currentChar = result.charAt(i);
values[i] = Character.toUpperCase(values[i]); if (Character.isLetter(currentChar) && Character.isLowerCase(currentChar)) {
result.setCharAt(i, Character.toUpperCase(currentChar));
} }
} }
return new String(values); return result.toString();
} }
} }