Removing Dollar and comma from string

前端 未结 11 1993
天涯浪人
天涯浪人 2020-12-14 10:47

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

String liveprice = \"$123,456.78\";
相关标签:
11条回答
  • 2020-12-14 11:17

    do like this

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

    output

    123456.78
    
    0 讨论(0)
  • 2020-12-14 11:17

    Here is more information Oracle JavaDocs:

    liveprice = liveprice.replace("X", "");
    
    0 讨论(0)
  • 2020-12-14 11:20

    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(",", "");
    
    0 讨论(0)
  • 2020-12-14 11:26

    Just use Replace instead

    String liveprice = "$123,456.78";
    String output = liveprice.replace("$", "");
    output = output .replace(",", "");
    
    0 讨论(0)
  • 2020-12-14 11:26

    I think that you could use regex. For example:

    "19.823.567,10 kr".replace(/\D/g, '')
    
    0 讨论(0)
  • 2020-12-14 11:27

    Will this works?

    String liveprice = "$123,456.78";
    String newStr = liveprice.replace("$", "").replace(",","");
    

    Output: 123456.78

    Live Demo

    Better One:

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

    Live Demo

    0 讨论(0)
提交回复
热议问题