Removing Dollar and comma from string

前端 未结 11 2047
天涯浪人
天涯浪人 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:40

    Example using Swedish Krona currency

    String x="19.823.567,10 kr";

            x=x.replace(".","");
            x=x.replaceAll("\\s+","");
            x=x.replace(",", ".");
            x=x.replaceAll("[^0-9 , .]", "");
    

    System.out.println(x);

    Will give the output ->19823567.10(which can now be used for any computation)

提交回复
热议问题