Java String Camel Case To camelToLowerSnake(String camel)

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

Description

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

License

Open Source License

Declaration

public static String camelToLowerSnake(String camel) 

Method Source Code

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

public class Main {
    /**/*from  w w  w. ja v a  2s. co  m*/
     * Converts camel case string (lower or upper/Pascal) to lower snake case,
     * for example 'helloWorld' or 'HelloWorld' -> 'hello_world' or 'HELLO_WORLD'.
     */
    public static String camelToLowerSnake(String camel) {
        return camelToSnake(camel, false);
    }

    /**
     * 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. camelizeOneWord(String word, boolean firstLetterInLowerCase)
  2. camelPrefix(String str, int prefixSize)
  3. camelToComposite(String camel)
  4. camelToFixedString(String str, String fixed)
  5. camelToLisp(final String pString)
  6. camelToPeriod(String value)
  7. camelToSplitName(String camelName, String split)
  8. camelToSql(String name)
  9. camelToWords(String camel)