Java - Write code to convert camel case to underscore

Requirements

Write code to convert camel case to underscore

Demo

//package com.book2s;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] argv) {
        String source = "AcamDef book2s.com";
        System.out.println(camel2Score(source));
    }/*from w w w .  j  a va 2  s  . c om*/

    public static String camel2Score(String source) {
        return pascal2Score(source);
    }

    public static String pascal2Score(String source) {
        String regexStr = "[A-Z]";
        Matcher matcher = Pattern.compile(regexStr).matcher(source);
        StringBuffer stringBuffer = new StringBuffer();
        while (matcher.find()) {
            String g = matcher.group();
            matcher.appendReplacement(stringBuffer, "_" + g.toLowerCase());
        }
        matcher.appendTail(stringBuffer);
        if (stringBuffer.charAt(0) == '_') {
            stringBuffer.delete(0, 1);
        }
        return stringBuffer.toString();
    }
}

Related Exercise