Java 该死!为什么这个简单的测试用例不起作用?

Java 该死!为什么这个简单的测试用例不起作用?,java,Java,此操作在显示wanttobeacamel后失败。为什么没有大写字符 @Test public void testCamelCase() { String orig="want_to_be_a_camel"; String camel=orig.replaceAll("_([a-z])", "$1".toUpperCase()); System.out.println(camel); assertEquals("wantToBeACamel", camel); }

此操作在显示wanttobeacamel后失败。为什么没有大写字符

@Test
public void testCamelCase() {
    String orig="want_to_be_a_camel";
    String camel=orig.replaceAll("_([a-z])", "$1".toUpperCase());
    System.out.println(camel);
    assertEquals("wantToBeACamel", camel);
}
========= 验尸:

使用简单的替代品是一条死胡同。我这么做只是为了好玩,教我的孩子写代码。。。但对于Jayamohan来说,他问道,这里有一个替代方法

java version "1.6.0_29"
Java(TM) SE Runtime Environment (build 1.6.0_29-b11)
Java HotSpot(TM) 64-Bit Server VM (build 20.4-b02, mixed mode)

我想这是因为1.5美元的价格比所有人都贵。由于$1实际上没有任何大写字母,因此它与$1相同。然后,当replaceAll运行时,后跟小写字母的模式下划线将被替换为小写字母。

我认为这是因为$1.toUpperCase在replaceAll之前运行。由于$1实际上没有任何大写字母,因此它与$1相同。然后,当replaceAll运行时,后跟小写字母的模式下划线被替换为小写字母。

我发现了我的错误。。。但我相信第一个能指出这一点的人。这个断言没有错。。。这就是我所期望的结果-一个camelCasedString.Psst,伙计告诉我,我会分享这个代表;提哈拉。嘿,干得不错。贾罗伊。。。不。没那么深奥。我发现了我的虫子。。。但我相信第一个能指出这一点的人。这个断言没有错。。。这就是我所期望的结果-一个camelCasedString.Psst,伙计告诉我,我会分享这个代表;提哈拉。嘿,干得不错。贾罗伊。。。不。没那么深奥。德诺先揍了你一拳,然后删除了他的回答。是的,我看到了——他以大约30秒左右的速度抓住了我。谢谢-+1为解决方案。如果你也发布修改过的src,那就太好了。德诺击败了你,但随后删除了他的答案。是的,我看到了,他以大约30秒左右的速度抓住了我。谢谢-+1为解决方案。如果你也发布修改过的src,那就太好了。
public String toCamelCase(String str) {
    if (str==null || str.length()==0) {
        return str;
    }
    char[] ar=str.toCharArray();
    int backref=0;
    ar[0]=Character.toLowerCase(ar[0]);
    for (int i=0; i<ar.length; i++) {
        if (ar[i]=='_') {
            ar[i-backref++]=Character.toUpperCase(ar[i+++1]);
        } else {
            ar[i-backref]=ar[i];
        }
    }
    return new String(ar).substring(0,ar.length-backref);
}