How can we remove dollar sign ($) and all comma(,) from same string? Would it be better to avoid regex?
String liveprice = "$123,456.78";
Try, String liveprice = "$123,456.78"; String newStr = liveprice. replaceAll("[$,]", "");
Use str. replace() to remove a comma from a string in Python Call str. replace(',', '') to replace every instance of a ',' in str with '' .
Using the substring() method We remove the last comma of a string by using the built-in substring() method with first argument 0 and second argument string. length()-1 in Java. Slicing starts from index 0 and ends before last index that is string. length()-1 .
Dollar signs can also be removed from a dataframe column or row, by using the gsub() method. All the instances of the $ sign are removed from the entries contained within the data frame.
do like this
NumberFormat format = NumberFormat.getCurrencyInstance();
Number number = format.parse("\$123,456.78");
System.out.println(number.toString());
output
123456.78
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(",", "");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With