Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing Dollar and comma from string

How can we remove dollar sign ($) and all comma(,) from same string? Would it be better to avoid regex?

String liveprice = "$123,456.78";
like image 225
Varun Vishnoi Avatar asked Dec 03 '13 12:12

Varun Vishnoi


People also ask

How do you remove the dollar sign from a string?

Try, String liveprice = "$123,456.78"; String newStr = liveprice. replaceAll("[$,]", "");

How do I remove a comma from a price string in Python?

Use str. replace() to remove a comma from a string in Python Call str. replace(',', '') to replace every instance of a ',' in str with '' .

How do you remove a comma at the end of a string?

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 .

How do I get rid of the dollar sign in R?

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.


2 Answers

do like this

NumberFormat format = NumberFormat.getCurrencyInstance();
Number number = format.parse("\$123,456.78");
System.out.println(number.toString());

output

123456.78
like image 155
Prabhakaran Ramaswamy Avatar answered Sep 19 '22 11:09

Prabhakaran Ramaswamy


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(",", "");
like image 32
Masudul Avatar answered Sep 21 '22 11:09

Masudul