Search code examples
javastringreplaceall

Whats wrong with this replaceAll()?


The output I get is once the value of x is printed and remaining two println prints blank lines.

1.234.567,89



Process finished with exit code 0

What am I doing wrong?

public class Dummy {

    public static void main(String args[]) {
        String x = "1.234.567,89 EUR";
        String e = " EUR";
        x = x.replaceAll(" EUR","");
        System.out.println(x);
        x = x.replaceAll(".", "");
        System.out.println(x);
        x = x.replaceAll(",",".");
        System.out.println(x);
           //System.out.println(x.replaceAll(" EUR","").replaceAll(".","").replaceAll(",","."));
    }
}

Solution

  • The problem is that x = x.replaceAll(".", ""); replaces every character with "" and therefore you have an empty x after the second replaceAll().

    Note that the first argument of the replaceAll() method is a regular expression.

    Change it to:

    x = x.replaceAll("\\.", "");