0

我正在尝试没有机会将 sql 属性转换为 java 格式。举个例子:我想将“p_start_date”更改为“pStartSate”。

我试过用

String var = "p_start_date";
var.replaceAll("(_[a-z])\1", "([A-Z])\1");

并且

Pattern pattern = Pattern.compile("([a-z0-9]+_)*");
        Matcher matcher = pattern.matcher(var);


    if (matcher.find()) {
        // Get all groups for this match
        //System.out.println(matcher.groupCount());
        for (int i=0; i<=matcher.groupCount(); i++) {
            String groupStr = matcher.group(i);
            System.out.println(groupStr);
        }
    }

但两者都不起作用

4

1 回答 1

1

这是你想要的?

String var = "p_start_date";

Pattern pattern = Pattern.compile("_([a-z])");
Matcher matcher = pattern.matcher(var);

StringBuffer sb=new StringBuffer();
while(matcher.find()) {
    matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
}
matcher.appendTail(sb);

System.out.println(sb);

输出:pStartDate

于 2012-08-08T09:32:29.843 回答