-3

如何转换字符串

 (1,234) 

成一个数

(1234) 

使用java?

4

6 回答 6

6

使用十进制格式

DecimalFormat  format = new DecimalFormat  ("#,###");
Number aNumber = format.parse("1,234");
System.out.println(aNumber.intValue());
于 2013-10-10T10:02:55.980 回答
2

你可以用NumberFormat

   String number = "1,234";
   NumberFormat numberFormat = NumberFormat.getInstance();
   int i = numberFormat.parse(number).intValue();
于 2013-10-10T10:02:34.850 回答
1

尝试String.repaceAll()

String value = "1,234";
System.out.println(Integer.parseInt(value.replaceAll(",", "")));
于 2013-10-10T09:56:29.287 回答
0

String is immutable, so when you do replaceAll, you need to reassign object to string reference,

String str = new String("1,234");
str = str.replaceAll(",", "");
System.out.println(Integer.parseInt(str));

This works fine when tested.

于 2013-10-10T10:04:21.680 回答
0
String str = new String("1,234");
    String str1=str.replace(",", "");
    Integer.parseInt(str1);

试试上面的代码

输出 1234

于 2013-10-10T10:04:06.863 回答
-2

如果速度是一个主要问题,您可能会很快发现类似的东西。它击败了这篇文章中的所有人。

int value(String s) {
  // Start at zero so first * 10 has no effect.
  int v = 0;
  // Work from the end of the string backwards.
  for ( int i = s.length() - 1; i >= 0; i-- ) {
    char c = s.charAt(i);
    // Ignore non-digits.
    if ( Character.isDigit(c)) {
      // Mul curent by 10 and add digit value.
      v = (v * 10) + (c - '0');
    }
  }
  return v;
}
于 2013-10-10T10:10:14.423 回答