Java String Camel Case to Snake Case camelToUpperSnake(String camel)

Here you can find the source of camelToUpperSnake(String camel)

Description

Converts camel case string (lower or upper/Pascal) to upper snake case, for example 'helloWorld' or 'HelloWorld' -> 'HELLO_WORLD'.

License

Open Source License

Declaration

public static String camelToUpperSnake(String camel) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//  ww  w . j  a  va  2  s .  c  o  m
     * Converts camel case string (lower or upper/Pascal) to upper snake case,
     * for example 'helloWorld' or 'HelloWorld' -> 'HELLO_WORLD'.
     */
    public static String camelToUpperSnake(String camel) {
        return camelToSnake(camel, true);
    }

    /**
     * Converts camel case string (lower or upper/Pascal) to snake case,
     * for example 'helloWorld' or 'HelloWorld' -> 'hello_world'.
     *
     * @param camel Input string.
     * @param upper True if result snake cased string should be upper cased like 'HELLO_WORLD'.
     */
    public static String camelToSnake(String camel, boolean upper) {
        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();
    }
}

Related

  1. camelToSnake(String value)
  2. camelToSnakeCase(final String camelCase)
  3. camelToSnakeCase(String camelcase)
  4. camelToSnakeCase(String string)
  5. camelToSneakCase(String name)