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.
*/
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;
}
char[] values = s.toCharArray();
for (int i = 0; i < values.length; ++i) {
if (Character.isLetter(values[i]) && Character.isLowerCase(values[i])) {
values[i] = Character.toUpperCase(values[i]);
StringBuilder result = new StringBuilder(s);
for (int i = 0; i < result.length(); ++i) {
char currentChar = result.charAt(i);
if (Character.isLetter(currentChar) && Character.isLowerCase(currentChar)) {
result.setCharAt(i, Character.toUpperCase(currentChar));
}
}
return new String(values);
return result.toString();
}
}