Convert a camel-case string to underscores - Java java.lang

Java examples for java.lang:String Camel Case

Description

Convert a camel-case string to underscores

Demo Code

//package com.java2s;

public class Main {
    /**//from w  w w  .j  a  v a  2 s .  c o  m
     * Convert a camel-case string to underscores
     *
     * @param string    the camel-case string to convert.
     * @return          an underscore version of the string specified.
     */
    public static String underscorize(String string) {
        String stripped = string.replaceAll(" ", "");
        String underscored = stripped.replaceAll("(.)([A-Z])", "$1_$2");
        return underscored.toLowerCase();
    }
}

Related Tutorials