Java Utililty Methods String Camel Case to Snake Case

List of utility methods to do String Camel Case to Snake Case

Description

The list of methods to do String Camel Case to Snake Case are organized into topic(s).

Method

StringcamelToSnake(final String camelStr)
camel To Snake
String ret = camelStr.replaceAll("([A-Z]+)([A-Z][a-z])", "$1_$2").replaceAll("([a-z])([A-Z])", "$1_$2");
return ret.toLowerCase();
StringcamelToSnake(String camel, boolean upper)
Converts camel case string (lower or upper/Pascal) to snake case, for example 'helloWorld' or 'HelloWorld' -> 'hello_world'.
StringBuilder stringBuilder = new StringBuilder();
for (char c : camel.toCharArray()) {
    char nc = upper ? Character.toUpperCase(c) : Character.toLowerCase(c);
    if (Character.isUpperCase(c)) {
        stringBuilder.append('_').append(nc);
    } else {
        stringBuilder.append(nc);
return stringBuilder.toString();
StringcamelToSnake(String value)
camel To Snake
final char[] name = value.toCharArray();
final StringBuilder builder = new StringBuilder();
for (int i = 0; i < name.length; i++) {
    if (Character.isUpperCase(name[i]) || name[i] == '.' || name[i] == '$') {
        if (i != 0 && name[i - 1] != '.' && name[i - 1] != '$') {
            builder.append('_');
        if (name[i] != '.' && name[i] != '$') {
...
StringcamelToSnakeCase(final String camelCase)
converts ruby to camel case
if (camelCase == null || camelCase.isEmpty()) {
    return "";
return camelCase.replaceAll("\\B([A-Z])", "_$1").toLowerCase();
StringcamelToSnakeCase(String camelcase)
camel To Snake Case
return camelcase.replaceAll("([A-Z][a-z])", "_$1");
StringcamelToSnakeCase(String string)
Converts a string from CamelCase to snake_case.
switch (string) {
case "IO":
    return "io";
case "OS":
    return "os";
case "MD5":
    return "md5";
default:
...
StringcamelToSneakCase(String name)
camel To Sneak Case
StringBuilder sb = new StringBuilder();
int cur = 0;
while (cur < name.length()) {
    char c = name.charAt(cur);
    if (!Character.isUpperCase(c)) {
        sb.append(c);
        cur++;
        continue;
...
StringcamelToUpperSnake(String camel)
Converts camel case string (lower or upper/Pascal) to upper snake case, for example 'helloWorld' or 'HelloWorld' -> 'HELLO_WORLD'.
return camelToSnake(camel, true);