如何转换字符串
(1,234)
成一个数
(1234)
使用java?
使用十进制格式
DecimalFormat format = new DecimalFormat ("#,###");
Number aNumber = format.parse("1,234");
System.out.println(aNumber.intValue());
你可以用NumberFormat
它
String number = "1,234";
NumberFormat numberFormat = NumberFormat.getInstance();
int i = numberFormat.parse(number).intValue();
String value = "1,234";
System.out.println(Integer.parseInt(value.replaceAll(",", "")));
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.
String str = new String("1,234");
String str1=str.replace(",", "");
Integer.parseInt(str1);
试试上面的代码
输出 1234
如果速度是一个主要问题,您可能会很快发现类似的东西。它击败了这篇文章中的所有人。
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;
}