Trying to remove all letters and characters that are not 0-9 and a period. I'm using Character.isDigit()
but it also removes decimal, how can I also keep the decimal?
试图删除所有非0-9和一个句点的字母和字符。我用的是字符。isdigit()但它也去掉了小数,我怎么能保留小数点呢?
8 个解决方案
#1
395
Try this code:
试试这段代码:
String str = "a12.334tyz.78x";
str = str.replaceAll("[^\\d.]", "");
Now str
will contain "12.334.78"
.
现在str将包含“12.334.78”。
#2
75
I would use a regex.
我会使用正则表达式。
String text = "-jaskdh2367sd.27askjdfh23";
String digits = text.replaceAll("[^0-9.]", "");
System.out.println(digits);
prints
打印
2367.2723
You might like to keep -
as well for negative numbers.
你可能想保留-对于负数也一样。
#3
25
String phoneNumberstr = "Tel: 00971-557890-999";
String numberRefined = phoneNumberstr.replaceAll("[^\\d-]", "");
result: 0097-557890-999
结果:0097-557890-999
if you also do not need "-" in String you can do like this:
如果你也不需要“-”字符串你可以这样做:
String phoneNumberstr = "Tel: 00971-55 7890 999";
String numberRefined = phoneNumberstr.replaceAll("[^0-9]", "");
result: 0097557890999
结果:0097557890999
#4
18
With guava:
番石榴:
String input = "abx123.5";
String result = CharMatcher.inRange('0', '9').or(CharMatcher.is('.')).retainFrom(input);
see http://code.google.com/p/guava-libraries/wiki/StringsExplained
参见http://code.google.com/p/guava-libraries/wiki/StringsExplained
#5
10
str = str.replaceAll("\\D+","");
#6
5
Simple way without using Regex:
不使用正则表达式的简单方法:
Adding an extra character check for dot '.'
will solve the requirement:
为点添加额外的字符检查。将解决以下要求:
public static String getOnlyNumerics(String str) {
if (str == null) {
return null;
}
StringBuffer strBuff = new StringBuffer();
char c;
for (int i = 0; i < str.length() ; i++) {
c = str.charAt(i);
if (Character.isDigit(c) || c == '.') {
strBuff.append(c);
}
}
return strBuff.toString();
}
#7
1
Currency decimal separator can be different from Locale to another. It could be dangerous to consider .
as separator always. i.e.
不同地区的货币小数分隔符可以不同。考虑它可能是危险的。作为分隔符。即。
╔════════════════╦═══════════════════╗
║ Currency ║ Sample ║
╠════════════════╬═══════════════════╣
║ USA ║ $1,222,333.44 USD ║
║ United Kingdom ║ £1.222.333,44 GBP ║
║ European ║ €1.333.333,44 EUR ║
╚════════════════╩═══════════════════╝
I think the proper way is:
我认为正确的方法是:
- Get decimal character via
DecimalFormatSymbols
by default Locale or specified one. - 通过缺省语言环境或指定语言环境通过DecimalFormatSymbols获取十进制字符。
- Cook regex pattern with decimal character in order to obtain digits only
- 用十进制字符烹调regex模式,以便只获得数字
And here how I am solving it:
我是这样解决的:
import java.text.DecimalFormatSymbols;
import java.util.Locale;
进口java.text.DecimalFormatSymbols;进口java.util.Locale;
code:
代码:
public static String getDigit(String quote, Locale locale) {
char decimalSeparator;
if (locale == null) {
decimalSeparator = new DecimalFormatSymbols().getDecimalSeparator();
} else {
decimalSeparator = new DecimalFormatSymbols(locale).getDecimalSeparator();
}
String regex = "[^0-9" + decimalSeparator + "]";
String valueOnlyDigit = quote.replaceAll(regex, "");
try {
return valueOnlyDigit;
} catch (ArithmeticException | NumberFormatException e) {
Log.e(TAG, "Error in getMoneyAsDecimal", e);
return null;
}
return null;
}
I hope that may help,'.
我希望这能有所帮助。
#8
0
A way to replace it with a java 8 stream:
用java 8流替换它的方法:
public static void main(String[] args) throws IOException
{
String test = "ab19198zxncvl1308j10923.";
StringBuilder result = new StringBuilder();
test.chars().mapToObj( i-> (char)i ).filter( c -> Character.isDigit(c) || c == '.' ).forEach( c -> result.append(c) );
System.out.println( result ); //returns 19198.130810923.
}
#1
395
Try this code:
试试这段代码:
String str = "a12.334tyz.78x";
str = str.replaceAll("[^\\d.]", "");
Now str
will contain "12.334.78"
.
现在str将包含“12.334.78”。
#2
75
I would use a regex.
我会使用正则表达式。
String text = "-jaskdh2367sd.27askjdfh23";
String digits = text.replaceAll("[^0-9.]", "");
System.out.println(digits);
prints
打印
2367.2723
You might like to keep -
as well for negative numbers.
你可能想保留-对于负数也一样。
#3
25
String phoneNumberstr = "Tel: 00971-557890-999";
String numberRefined = phoneNumberstr.replaceAll("[^\\d-]", "");
result: 0097-557890-999
结果:0097-557890-999
if you also do not need "-" in String you can do like this:
如果你也不需要“-”字符串你可以这样做:
String phoneNumberstr = "Tel: 00971-55 7890 999";
String numberRefined = phoneNumberstr.replaceAll("[^0-9]", "");
result: 0097557890999
结果:0097557890999
#4
18
With guava:
番石榴:
String input = "abx123.5";
String result = CharMatcher.inRange('0', '9').or(CharMatcher.is('.')).retainFrom(input);
see http://code.google.com/p/guava-libraries/wiki/StringsExplained
参见http://code.google.com/p/guava-libraries/wiki/StringsExplained
#5
10
str = str.replaceAll("\\D+","");
#6
5
Simple way without using Regex:
不使用正则表达式的简单方法:
Adding an extra character check for dot '.'
will solve the requirement:
为点添加额外的字符检查。将解决以下要求:
public static String getOnlyNumerics(String str) {
if (str == null) {
return null;
}
StringBuffer strBuff = new StringBuffer();
char c;
for (int i = 0; i < str.length() ; i++) {
c = str.charAt(i);
if (Character.isDigit(c) || c == '.') {
strBuff.append(c);
}
}
return strBuff.toString();
}
#7
1
Currency decimal separator can be different from Locale to another. It could be dangerous to consider .
as separator always. i.e.
不同地区的货币小数分隔符可以不同。考虑它可能是危险的。作为分隔符。即。
╔════════════════╦═══════════════════╗
║ Currency ║ Sample ║
╠════════════════╬═══════════════════╣
║ USA ║ $1,222,333.44 USD ║
║ United Kingdom ║ £1.222.333,44 GBP ║
║ European ║ €1.333.333,44 EUR ║
╚════════════════╩═══════════════════╝
I think the proper way is:
我认为正确的方法是:
- Get decimal character via
DecimalFormatSymbols
by default Locale or specified one. - 通过缺省语言环境或指定语言环境通过DecimalFormatSymbols获取十进制字符。
- Cook regex pattern with decimal character in order to obtain digits only
- 用十进制字符烹调regex模式,以便只获得数字
And here how I am solving it:
我是这样解决的:
import java.text.DecimalFormatSymbols;
import java.util.Locale;
进口java.text.DecimalFormatSymbols;进口java.util.Locale;
code:
代码:
public static String getDigit(String quote, Locale locale) {
char decimalSeparator;
if (locale == null) {
decimalSeparator = new DecimalFormatSymbols().getDecimalSeparator();
} else {
decimalSeparator = new DecimalFormatSymbols(locale).getDecimalSeparator();
}
String regex = "[^0-9" + decimalSeparator + "]";
String valueOnlyDigit = quote.replaceAll(regex, "");
try {
return valueOnlyDigit;
} catch (ArithmeticException | NumberFormatException e) {
Log.e(TAG, "Error in getMoneyAsDecimal", e);
return null;
}
return null;
}
I hope that may help,'.
我希望这能有所帮助。
#8
0
A way to replace it with a java 8 stream:
用java 8流替换它的方法:
public static void main(String[] args) throws IOException
{
String test = "ab19198zxncvl1308j10923.";
StringBuilder result = new StringBuilder();
test.chars().mapToObj( i-> (char)i ).filter( c -> Character.isDigit(c) || c == '.' ).forEach( c -> result.append(c) );
System.out.println( result ); //returns 19198.130810923.
}