-1

Maybe I am using replaceAll incorrectly, but I am unable to find why it acts this way. I want to simply remove a $ sign from a string and then output the string.

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

However, this still outputs the $ symbol with the string. Does anyone know why this is happening?

Sythe
  • 143
  • 1
  • 1
  • 9

1 Answers1

3

You need to assign the return value of replaceAll to a variable:

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

because a String object is immutable.

M A
  • 71,713
  • 13
  • 134
  • 174