java 判断是否为数字
java
老外很厉害, 茴香豆有好多中写法, 是目前看到最全的.
https://www.baeldung.com/java-check-string-number
https://javarevisited.blogspot.com/2016/10/how-to-check-if-string-is-numeric-in-Java.html
Using Plain Java
Perhaps the easiest and the most reliable way to check whether a String is numeric or not is by parsing it using Java's built-in methods:
- Integer.parseInt(String)
- Float.parseFloat(String)
- Double.parseDouble(String)
- Long.parseLong(String)
- new BigInteger(String)
If these methods don't throw any NumberFormatException, then it means that the parsing was successful and the String is numeric:
1
2
3
4
5
6
7
8
9
10
11
|
public static boolean isNumeric(String strNum) { if (strNum == null ) { return false ; } try { double d = Double.parseDouble(strNum); } catch (NumberFormatException nfe) { return false ; } return true ; } |
Let's see this method in action:
1
2
3
4
5
6
7
8
9
|
assertThat(isNumeric( "22" )).isTrue(); assertThat(isNumeric( "5.05" )).isTrue(); assertThat(isNumeric( "-200" )).isTrue(); assertThat(isNumeric( "10.0d" )).isTrue(); assertThat(isNumeric( " 22 " )).isTrue(); assertThat(isNumeric( null )).isFalse(); assertThat(isNumeric( "" )).isFalse(); assertThat(isNumeric( "abc" )).isFalse(); |
In our isNumeric() method, we're just checking for values that are of type Double, but this method can also be modified to check for Integer, Float, Long and large numbers by using any of the parse methods that we have enlisted earlier.
These methods are also discussed in the Java String Conversions article.
4. Using Regular Expressions
Now let's use regex -?\d+(\.\d+)? to match numeric Strings consisting of the positive or negative integer and floats.
Let’s break down this regex and see how it works:
- -? – this part identifies if the given number is negative, the dash “–” searches for dash literally and the question mark “?” marks its presence as an optional one
- \d+ – this searches for one or more digits
- (\.\d+)? – this part of regex is to identify float numbers. Here we're searching for one or more digits followed by a period. The question mark, in the end, signifies that this complete group is optional
Regular expressions are a very broad topic. To get a brief overview, check our tutorial on the Java regular expressions API.
For now, let's create a method using the above regular expression:
1
2
3
4
5
6
7
8
|
private Pattern pattern = Pattern.compile( "-?\\d+(\\.\\d+)?" ); public boolean isNumeric(String strNum) { if (strNum == null ) { return false ; } return pattern.matcher(strNum).matches(); } |
Let's now look at some assertions for the above method:
1
2
3
4
5
6
|
assertThat(isNumeric( "22" )).isTrue(); assertThat(isNumeric( "5.05" )).isTrue(); assertThat(isNumeric( "-200" )).isTrue(); assertThat(isNumeric( null )).isFalse(); assertThat(isNumeric( "abc" )).isFalse(); |
5. Using Apache Commons
In this section, we'll discuss various methods available in the Apache Commons library.
5.1. NumberUtils.isCreatable(String)
NumberUtils from Apache Commons provides a static method NumberUtils.isCreatable(String) which checks whether a String is a valid Java number or not.
This method accepts:
- Hexadecimal numbers starting with 0x or 0X
- Octal numbers starting with a leading 0
- Scientific notation (for example 1.05e-10)
- Numbers marked with a type qualifier (for example 1L or 2.2d)
If the supplied string is null or empty/blank, then it's not considered a number and the method will return false.
Let's run some tests using this method:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
assertThat(NumberUtils.isCreatable( "22" )).isTrue(); assertThat(NumberUtils.isCreatable( "5.05" )).isTrue(); assertThat(NumberUtils.isCreatable( "-200" )).isTrue(); assertThat(NumberUtils.isCreatable( "10.0d" )).isTrue(); assertThat(NumberUtils.isCreatable( "1000L" )).isTrue(); assertThat(NumberUtils.isCreatable( "0xFF" )).isTrue(); assertThat(NumberUtils.isCreatable( "07" )).isTrue(); assertThat(NumberUtils.isCreatable( "2.99e+8" )).isTrue(); assertThat(NumberUtils.isCreatable( null )).isFalse(); assertThat(NumberUtils.isCreatable( "" )).isFalse(); assertThat(NumberUtils.isCreatable( "abc" )).isFalse(); assertThat(NumberUtils.isCreatable( " 22 " )).isFalse(); assertThat(NumberUtils.isCreatable( "09" )).isFalse(); |
Note how we're getting true assertions for hexadecimal numbers, octal numbers and scientific notations in lines 6, 7 and 8 respectively.
Also, on line 14, the string “09” returns false because the preceding “0” indicates that this is an octal number and “09” is not a valid octal number.
For every input that returns true with this method, we can use NumberUtils.createNumber(String) which will give us the valid number.
5.2. NumberUtils.isParsable(String)
The NumberUtils.isParsable(String) method checks whether the given String is parsable or not.
Parsable numbers are those that are parsed successfully by any parse method like Integer.parseInt(String), Long.parseLong(String), Float.parseFloat(String) or Double.parseDouble(String).
Unlike NumberUtils.isCreatable(), this method won't accept hexadecimal numbers, scientific notations or strings ending with any type qualifier, that is, ‘f', ‘F', ‘d' ,'D' ,'l'or‘L'.
Let's look at some affirmations:
1
2
3
4
5
6
7
8
9
10
11
12
|
assertThat(NumberUtils.isParsable( "22" )).isTrue(); assertThat(NumberUtils.isParsable( "-23" )).isTrue(); assertThat(NumberUtils.isParsable( "2.2" )).isTrue(); assertThat(NumberUtils.isParsable( "09" )).isTrue(); assertThat(NumberUtils.isParsable( null )).isFalse(); assertThat(NumberUtils.isParsable( "" )).isFalse(); assertThat(NumberUtils.isParsable( "6.2f" )).isFalse(); assertThat(NumberUtils.isParsable( "9.8d" )).isFalse(); assertThat(NumberUtils.isParsable( "22L" )).isFalse(); assertThat(NumberUtils.isParsable( "0xFF" )).isFalse(); assertThat(NumberUtils.isParsable( "2.99e+8" )).isFalse(); |
On line 4, unlike NumberUtils.isCreatable(), the number starting with string “0” isn't considered as an octal number, but a normal decimal number and hence it returns true.
We can use this method as a replacement for what we did in section 3, where we’re trying to parse a number and checking for an error.
5.3. StringUtils.isNumeric(CharSequence)
The method StringUtils.isNumeric(CharSequence) checks strictly for Unicode digits. This means:
- Any digits from any language that is a Unicode digit is acceptable
- Since a decimal point is not considered as a Unicode digit, it's not valid
- Leading signs (either positive or negative) are also not acceptable
Let's now see this method in action:
1
2
3
4
5
6
7
8
9
10
11
|
assertThat(StringUtils.isNumeric( "123" )).isTrue(); assertThat(StringUtils.isNumeric( "١٢٣" )).isTrue(); assertThat(StringUtils.isNumeric( "१२३" )).isTrue(); assertThat(StringUtils.isNumeric( null )).isFalse(); assertThat(StringUtils.isNumeric( "" )).isFalse(); assertThat(StringUtils.isNumeric( " " )).isFalse(); assertThat(StringUtils.isNumeric( "12 3" )).isFalse(); assertThat(StringUtils.isNumeric( "ab2c" )).isFalse(); assertThat(StringUtils.isNumeric( "12.3" )).isFalse(); assertThat(StringUtils.isNumeric( "-123" )).isFalse(); |
Note that the input parameters in lines 2 and 3 are representing numbers 123 in Arabic and Devanagari respectively. Since they're valid Unicode digits, this method returns true on them.
The StringUtils.isNumericSpace(CharSequence) checks strictly for Unicode digits and/or space. This is same as StringUtils.isNumeric() with the only difference being that it accepts spaces as well, not only leading and trailing spaces but also if they're in between numbers:
1
2
3
4
5
6
7
8
9
10
|
assertThat(StringUtils.isNumericSpace( "123" )).isTrue(); assertThat(StringUtils.isNumericSpace( "١٢٣" )).isTrue(); assertThat(StringUtils.isNumericSpace( "" )).isTrue(); assertThat(StringUtils.isNumericSpace( " " )).isTrue(); assertThat(StringUtils.isNumericSpace( "12 3" )).isTrue(); assertThat(StringUtils.isNumericSpace( null )).isFalse(); assertThat(StringUtils.isNumericSpace( "ab2c" )).isFalse(); assertThat(StringUtils.isNumericSpace( "12.3" )).isFalse(); assertThat(StringUtils.isNumericSpace( "-123" )).isFalse(); |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现