How can we remove dollar sign ($) and all comma(,) from same string? Would it be better to avoid regex?
String liveprice = \"$123,456.78\";
do like this
NumberFormat format = NumberFormat.getCurrencyInstance();
Number number = format.parse("\$123,456.78");
System.out.println(number.toString());
output
123456.78
Here is more information Oracle JavaDocs:
liveprice = liveprice.replace("X", "");
Try,
String liveprice = "$123,456.78";
String newStr = liveprice.replaceAll("[$,]", "");
replaceAll
uses regex, to avoid regex than try with consecutive replace
method.
String liveprice = "$1,23,456.78";
String newStr = liveprice.replace("$", "").replace(",", "");
Just use Replace
instead
String liveprice = "$123,456.78";
String output = liveprice.replace("$", "");
output = output .replace(",", "");
I think that you could use regex. For example:
"19.823.567,10 kr".replace(/\D/g, '')
Will this works?
String liveprice = "$123,456.78";
String newStr = liveprice.replace("$", "").replace(",","");
Output: 123456.78
Better One:
String liveprice = "$123,456.78";
String newStr = liveprice.replaceAll("[$,]", "")