Java String Camel Case to Snake Case camelToSnakeCase(String string)

Here you can find the source of camelToSnakeCase(String string)

Description

Converts a string from CamelCase to snake_case.

License

Apache License

Declaration

public static String camelToSnakeCase(String string) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**//from w  ww .ja  va2 s.c  o m
     * Converts a string from CamelCase to snake_case.
     */
    public static String camelToSnakeCase(String string) {
        switch (string) {
        case "IO":
            return "io";
        case "OS":
            return "os";
        case "MD5":
            return "md5";
        default:
            StringBuilder result = new StringBuilder(string.length());
            for (char c : string.toCharArray()) {
                if (Character.isUpperCase(c)) {
                    if (result.length() > 0) {
                        result.append('_');
                    }
                    result.append(Character.toLowerCase(c));
                } else {
                    result.append(c);
                }
            }
            return result.toString();
        }
    }
}

Related

  1. camelToSnake(final String camelStr)
  2. camelToSnake(String camel, boolean upper)
  3. camelToSnake(String value)
  4. camelToSnakeCase(final String camelCase)
  5. camelToSnakeCase(String camelcase)
  6. camelToSneakCase(String name)
  7. camelToUpperSnake(String camel)