Java String Underscore underscore(String camel)

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

Description

Converts a CamelCase string to underscores: "AliceInWonderLand" becomes: "alice_in_wonderland"

License

LGPL

Parameter

Parameter Description
camel camel case input

Return

result converted to underscores.

Declaration

public static String underscore(String camel) 

Method Source Code


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

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**// w  w  w.ja  v  a  2s  . c  o m
     * Converts a CamelCase string to underscores: "AliceInWonderLand" becomes:
     * "alice_in_wonderland"
     *
     * @param camel camel case input
     * @return result converted to underscores.
     */
    public static String underscore(String camel) {

        List<Integer> upper = new ArrayList<Integer>();
        byte[] bytes = camel.getBytes();
        for (int i = 0; i < bytes.length; i++) {
            byte b = bytes[i];
            if (b < 97 || b > 122) {
                upper.add(i);
            }
        }

        StringBuffer b = new StringBuffer(camel);
        for (int i = upper.size() - 1; i >= 0; i--) {
            Integer index = upper.get(i);
            if (index != 0)
                b.insert(index, "_");
        }

        return b.toString().toLowerCase();
    }
}

Related

  1. toUnderscoredLowercase(String text)
  2. toUnderscoreName(String name)
  3. toUnderscores(String s)
  4. toUnderscoreSeparated(String name)
  5. underscore(final String s, final Locale locale)
  6. underscore(String camelCaseWord)
  7. underscore(String field)
  8. underscore(String phase)
  9. underscore(String str)