Reputation: 11234
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(",","."));
}
}
Upvotes: 1
Views: 192
Reputation: 38132
As an alternative solution:
Consider to use NumberFormat.getCurrencyInstance or DecimalFormat. NumberFormat provides a parse method.
E.g. 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);
}
Upvotes: 0
Reputation: 62864
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("\\.", "");
Upvotes: 9
Reputation: 35557
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|\\.|,",""));
Upvotes: 1
Reputation: 31567
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);
}
Upvotes: 0
Reputation: 213223
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(",",".");
Upvotes: 3
Reputation: 95948
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:
replace
that accepts a Strings.
by \\.
(Escaping regex is done by \
but in Java \
is written \\
)Upvotes: 0