从字符串中删除元和逗号

时间:2021-04-23 16:50:42

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

如何从相同的字符串中删除元号($)和所有逗号(,)?避免regex会更好吗?

String liveprice = "$123,456.78";

9 个解决方案

#1


21  

do like this

这样做

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

output

输出

123456.78

#2


13  

Try,

试,

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

replaceAll uses regex, to avoid regex than try with consecutive replace method.

替换所有使用regex,以避免regex,而不使用连续替换方法。

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

#3


1  

Just use Replace instead

只使用替换而不是

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

#4


1  

Without regex, you can try this:

没有regex,您可以尝试以下操作:

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

#5


0  

Here is more information Oracle JavaDocs:

下面是Oracle JavaDocs的更多信息:

liveprice = liveprice.replace("X", "");

#6


0  

Is a replace really what you need?

你真的需要一个替代品吗?

public void test() {
  String s = "$123,456.78";
  StringBuilder t = new StringBuilder();
  for ( int i = 0; i < s.length(); i++ ) {
    char ch = s.charAt(i);
    if ( Character.isDigit(ch)) {
      t.append(ch);
    }
  }
}

This will work for any decorated number.

这适用于任何修饰过的数字。

#7


0  

Will this works?

这工作吗?

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

Output: 123456.78

输出:123456.78

Live Demo

Better One:

更好的一个:

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

Live Demo

#8


0  

Example using Swedish Krona currency

使用瑞典克朗货币的例子

String x="19.823.567,10 kr";

字符串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);

System.out.println(x);

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

将输出->19823567.10(现在可以用于任何计算)

#9


-1  

I think that you could use regex. For example:

我想你可以用regex。例如:

"19.823.567,10 kr".replace(/\D/g, '')

#1


21  

do like this

这样做

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

output

输出

123456.78

#2


13  

Try,

试,

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

replaceAll uses regex, to avoid regex than try with consecutive replace method.

替换所有使用regex,以避免regex,而不使用连续替换方法。

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

#3


1  

Just use Replace instead

只使用替换而不是

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

#4


1  

Without regex, you can try this:

没有regex,您可以尝试以下操作:

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

#5


0  

Here is more information Oracle JavaDocs:

下面是Oracle JavaDocs的更多信息:

liveprice = liveprice.replace("X", "");

#6


0  

Is a replace really what you need?

你真的需要一个替代品吗?

public void test() {
  String s = "$123,456.78";
  StringBuilder t = new StringBuilder();
  for ( int i = 0; i < s.length(); i++ ) {
    char ch = s.charAt(i);
    if ( Character.isDigit(ch)) {
      t.append(ch);
    }
  }
}

This will work for any decorated number.

这适用于任何修饰过的数字。

#7


0  

Will this works?

这工作吗?

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

Output: 123456.78

输出:123456.78

Live Demo

Better One:

更好的一个:

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

Live Demo

#8


0  

Example using Swedish Krona currency

使用瑞典克朗货币的例子

String x="19.823.567,10 kr";

字符串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);

System.out.println(x);

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

将输出->19823567.10(现在可以用于任何计算)

#9


-1  

I think that you could use regex. For example:

我想你可以用regex。例如:

"19.823.567,10 kr".replace(/\D/g, '')