简体   繁体   中英

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(",","."));
    }
}

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("\\.", "");

String#replaceAll() method takes a regex as first parameter. And a . in regex matches any character except newline. That is why it is replacing everything.

You can just use String#replace() instead.

x = x.replace(" EUR","");
System.out.println(x);
x = x.replace(".", "");
System.out.println(x);
x = x.replace(",",".");

Use

System.out.println(x.replaceAll(" EUR","").replaceAll("\\.","")
                                                 .replaceAll(",","."));

instead of

System.out.println(x.replaceAll(" EUR","").replaceAll(".","")
                                                 .replaceAll(",","."));

You have to scape . with \\\\.

You can do this in single line as follows

System.out.println(x.replaceAll(" EUR|\\.|,",""));

Use Pattern#quote :

x = x.replaceAll(Pattern.quote("."), "");

To tell Java that . is not the regex . that has a special meaning, but the String . .

Other solutions:

  • Use replace that accepts a Strings
  • Escape the . by \\\\. (Escaping regex is done by \\ but in Java \\ is written \\\\ )

Read JavaDoc from String.replaceAll(String regex, String replacement)

regex

  • the regular expression to which this string is to be matched

The dot ( . ) matches (almost) any character. To escape dot use backslash ( \\ ), Java needs double backslash ( \\\\ ).

Your fixed code after escaping dot looks like this.

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);        
}

As an alternative solution:

Consider to use NumberFormat.getCurrencyInstance or DecimalFormat . NumberFormat provides a parse method.

Eg try:

final NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(Locale.GERMANY);
if (currencyFormat instanceof DecimalFormat) {
    final DecimalFormat currencyDecimalFormat = (DecimalFormat) currencyFormat;

    final DecimalFormatSymbols decimalFormatSymbols = currencyDecimalFormat.getDecimalFormatSymbols();
    decimalFormatSymbols.setCurrencySymbol("EUR");
    currencyDecimalFormat.setDecimalFormatSymbols(decimalFormatSymbols);

    currencyDecimalFormat.setParseBigDecimal(true);

    System.out.println(currencyFormat.format(new BigDecimal("1234567.89")));
    final BigDecimal number = (BigDecimal) currencyFormat.parse("1.234.567,89 EUR");
    System.out.println(number);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM