Java String Camel Case To camel2underline(String hump)

Here you can find the source of camel2underline(String hump)

Description

camelunderline

License

Open Source License

Declaration

public static String camel2underline(String hump) 

Method Source Code

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

public class Main {
    private static final int alphaStep = 'a' - 'A';

    public static String camel2underline(String hump) {
        StringBuilder underline = new StringBuilder();
        char[] chars = hump.trim().toCharArray();

        for (int i = 0; i < chars.length; i++) {
            char c = chars[i];
            if (i == 0)
                underline.append(toUpperCase(c));
            else {
                if (isUpperCase(c)) {
                    underline.append('_');
                    underline.append(c);
                } else
                    underline.append(toUpperCase(c));
            }/*from w ww  .ja  va  2 s.  c  o m*/
        }

        return underline.toString();
    }

    public static char toUpperCase(char c) {
        return isLowerCase(c) ? (char) (c - alphaStep) : c;
    }

    public static boolean isUpperCase(char c) {
        return c >= 'A' && c <= 'Z';
    }

    public static boolean isLowerCase(char c) {
        return c >= 'a' && c <= 'z';
    }
}

Related

  1. camel2underline(String s)
  2. camelCase2Delimiter(String name, char delimiter)
  3. camelCaseIt(String mappingName, String separator)
  4. camelCaseMatch(String word, String abbr)