Java String Underscore toUnderscoreName(String name)

Here you can find the source of toUnderscoreName(String name)

Description

Convert a name in camelCase to an underscored name in lower case.

License

Apache License

Parameter

Parameter Description
filteredName the string containing original name

Return

the converted name

Declaration

public static String toUnderscoreName(String name) 

Method Source Code

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

public class Main {
    /**//ww  w.j av  a 2s .c om
     * Convert a name in camelCase to an underscored name in lower case.
     * Any upper case letters are converted to lower case with a preceding underscore.
     * @param filteredName the string containing original name
     * @return the converted name
     */
    public static String toUnderscoreName(String name) {
        if (name == null)
            return null;

        String filteredName = name;
        if (filteredName.indexOf("_") >= 0
                && filteredName.equals(filteredName.toUpperCase())) {
            filteredName = filteredName.toLowerCase();
        }
        if (filteredName.indexOf("_") == -1
                && filteredName.equals(filteredName.toUpperCase())) {
            filteredName = filteredName.toLowerCase();
        }

        StringBuffer result = new StringBuffer();
        if (filteredName != null && filteredName.length() > 0) {
            result.append(filteredName.substring(0, 1).toLowerCase());
            for (int i = 1; i < filteredName.length(); i++) {
                String preChart = filteredName.substring(i - 1, i);
                String c = filteredName.substring(i, i + 1);
                if (c.equals("_")) {
                    result.append("_");
                    continue;
                }
                if (preChart.equals("_")) {
                    result.append(c.toLowerCase());
                    continue;
                }
                if (c.matches("\\d")) {
                    result.append(c);
                } else if (c.equals(c.toUpperCase())) {
                    result.append("_");
                    result.append(c.toLowerCase());
                } else {
                    result.append(c);
                }
            }
        }
        return result.toString();
    }
}

Related

  1. toUnderscoreCase(String name)
  2. toUnderScoreCase(String s)
  3. toUnderScoreCase(String s)
  4. toUnderscored(String name)
  5. toUnderscoredLowercase(String text)
  6. toUnderscores(String s)
  7. toUnderscoreSeparated(String name)
  8. underscore(final String s, final Locale locale)
  9. underscore(String camel)