【问题标题】:Having issues with replaceAll() [duplicate]replaceAll() 有问题[重复]
【发布时间】:2015-10-08 19:30:49
【问题描述】:

也许我错误地使用了 replaceAll,但我无法找到它为什么会这样。我想简单地从字符串中删除 $ 符号,然后输出字符串。

public class Example{
  public static void main(String[] args){
    String s = "$50";
    s.replaceAll("\\D+", "");
    System.out.println(s);
  }
}

但是,这仍然会输出带有字符串的 $ 符号。有谁知道为什么会这样?

【问题讨论】:

    标签: java regex replaceall


    【解决方案1】:

    你需要将replaceAll的返回值赋给一个变量:

    s = s.replaceAll("\\D+", "");
    

    因为String 对象是不可变的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-08
      • 1970-01-01
      • 2016-09-14
      • 2013-01-08
      • 2011-03-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多