Java 驼峰字符串转下划线连接

代码:

	@Test
    public void test8() {
        String str = "userLevelAndAge";
        String res = String.join("_", str.replaceAll("([A-Z])", ",$1").split(",")).toLowerCase();
        // 更简洁的写法,感谢qq_148761779同学
        String res = str.replaceAll("([A-Z])", "_$1").toLowerCase();
        System.out.println(res);
    }

输出:

user_level_and_age

你可能感兴趣的:(Java)