Java笔试题(6)

String str = "my.test.";

String newStr = str.replaceAll(".", "/") + "end";

System.out.println(newStr);
  

输出结果?

我做的时候选的答案输出为   my/test/end

正确答案应为    ////////end

因为replaceAll第一个参数是正则表达式的字符串,"."在正则表达式中代表所有字符,下面为该方法的代码

public String replaceAll(String regex, String replacement) {
    return Pattern.compile(regex).matcher(this).replaceAll(replacement);
}

猜你喜欢

转载自www.cnblogs.com/myibu/p/9692301.html