Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert camelCaseString to UPPERCASE_STRING_WITH_UNDERSCORE

Tags:

java

Does anyone know of a library that would allow me to convert a camel case string to an uppercase with underscore string?

e.g. addressId ==> ADDRESS_ID

like image 483
Justin Kredible Avatar asked Jan 19 '23 08:01

Justin Kredible


1 Answers

You can create your own method:

public static String toUpperCaseWithUnderscore(String input) {
    if(input == null) {
        throw new IllegalArgumentException();
    }

    StringBuilder sb = new StringBuilder();
    for(int i = 0; i < input.length(); i++) {
        char c = input.charAt(i);
        if(Character.isUpperCase(c)) {
            if(i > 0) {
                sb.append('_');
            }
            sb.append(c);
        } else {
            sb.append(Character.toUpperCase(c));
        }
    }

    return sb.toString();
}
like image 57
The Scrum Meister Avatar answered Feb 07 '23 19:02

The Scrum Meister