驼峰字符串转下划线字符串小工具

HelloWorld-->hello_world   不废话

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by albert.bai on 2014/11/24.
 */
public class TestMatcher {
    public static String camelCaseUnderScope(String param) {
        Pattern p = Pattern.compile("[A-Z]");
        if (param == null || param.equals("")) {
            return "";
        }
        StringBuilder stringBuilder = new StringBuilder(param);
        Matcher mc = p.matcher(param);
        int i = 0;
        while (mc.find()) {
            int j = mc.start(), t = mc.end();
            stringBuilder.replace(j + i, t + i, "_" + mc.group().toLowerCase());
            System.out.println(j + " " + (j + i));
            System.out.println(t + " " + (t + i));
            i++;
            System.out.println(i);
        }
        if ('_' == stringBuilder.charAt(0)) {
            stringBuilder.deleteCharAt(0);
        }
        return stringBuilder.toString();
    }

    public static void main(String[] args) {
        System.out.println(camelCaseUnderScope("helloWorldBaiHao"));

        StringBuilder stringBuffer = new StringBuilder("Java Util Package");
        stringBuffer.replace(5, 9, "Lang");
        System.out.println(stringBuffer.toString());
    }
}


你可能感兴趣的:(Java,java)