我通常使用以下习惯来检查String是否可以转换为整数.
public boolean isInteger( String input ) {
try {
Integer.parseInt( input );
return true;
}
catch( Exception e ) {
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
它只是我,还是看起来有点hackish?什么是更好的方式?
看看我的回答(基于CodingWithSpike的早期答案的基准),看看为什么我改变了我的立场并接受了Jonas Klemming对这个问题的回答.我认为这个原始代码将被大多数人使用,因为它实现起来更快,更易于维护,但是当提供非整数数据时,它会慢一个数量级.
Jon*_*ing 160
如果您不关心潜在的溢出问题,此功能的使用速度比使用速度快20-30倍Integer.parseInt()
.
public static boolean isInteger(String str) {
if (str == null) {
return false;
}
int length = str.length();
if (length == 0) {
return false;
}
int i = 0;
if (str.charAt(0) == '-') {
if (length == 1) {
return false;
}
i = 1;
}
for (; i < length; i++) {
char c = str.charAt(i);
if (c < '0' || c > '9') {
return false;
}
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
Cod*_*ike 36
做了一个快速的基准.除非你开始弹出多个方法并且JVM必须做很多工作才能使执行堆栈到位,否则异常实际上并不是那种费用.当保持相同的方法时,他们的表现并不差.
public void RunTests()
{
String str = "1234567890";
long startTime = System.currentTimeMillis();
for(int i = 0; i < 100000; i++)
IsInt_ByException(str);
long endTime = System.currentTimeMillis();
System.out.print("ByException: ");
System.out.println(endTime - startTime);
startTime = System.currentTimeMillis();
for(int i = 0; i < 100000; i++)
IsInt_ByRegex(str);
endTime = System.currentTimeMillis();
System.out.print("ByRegex: ");
System.out.println(endTime - startTime);
startTime = System.currentTimeMillis();
for(int i = 0; i < 100000; i++)
IsInt_ByJonas(str);
endTime = System.currentTimeMillis();
System.out.print("ByJonas: ");
System.out.println(endTime - startTime);
}
private boolean IsInt_ByException(String str)
{
try
{
Integer.parseInt(str);
return true;
}
catch(NumberFormatException nfe)
{
return false;
}
}
private boolean IsInt_ByRegex(String str)
{
return str.matches("^-?\\d+$");
}
public boolean IsInt_ByJonas(String str)
{
if (str == null) {
return false;
}
int length = str.length();
if (length == 0) {
return false;
}
int i = 0;
if (str.charAt(0) == '-') {
if (length == 1) {
return false;
}
i = 1;
}
for (; i < length; i++) {
char c = str.charAt(i);
if (c <= '/' || c >= ':') {
return false;
}
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
输出:
ByException:31
ByRegex:453(注意:每次重新编译模式)
ByJonas:16
我同意Jonas K的解决方案也是最强大的.看起来他赢了:)
Fel*_*ipe 35
因为有可能人们仍然访问这里并且在基准测试后会对Regex产生偏见......所以我将提供一个基准测试的更新版本,以及Regex的编译版本.与之前的基准测试相反,这一点显示Regex解决方案实际上具有始终如一的良好性能.
从比尔蜥蜴复制并更新编译版本:
private final Pattern pattern = Pattern.compile("^-?\\d+$");
public void runTests() {
String big_int = "1234567890";
String non_int = "1234XY7890";
long startTime = System.currentTimeMillis();
for(int i = 0; i < 100000; i++)
IsInt_ByException(big_int);
long endTime = System.currentTimeMillis();
System.out.print("ByException - integer data: ");
System.out.println(endTime - startTime);
startTime = System.currentTimeMillis();
for(int i = 0; i < 100000; i++)
IsInt_ByException(non_int);
endTime = System.currentTimeMillis();
System.out.print("ByException - non-integer data: ");
System.out.println(endTime - startTime);
startTime = System.currentTimeMillis();
for(int i = 0; i < 100000; i++)
IsInt_ByRegex(big_int);
endTime = System.currentTimeMillis();
System.out.print("\nByRegex - integer data: ");
System.out.println(endTime - startTime);
startTime = System.currentTimeMillis();
for(int i = 0; i < 100000; i++)
IsInt_ByRegex(non_int);
endTime = System.currentTimeMillis();
System.out.print("ByRegex - non-integer data: ");
System.out.println(endTime - startTime);
startTime = System.currentTimeMillis();
for (int i = 0; i < 100000; i++)
IsInt_ByCompiledRegex(big_int);
endTime = System.currentTimeMillis();
System.out.print("\nByCompiledRegex - integer data: ");
System.out.println(endTime - startTime);
startTime = System.currentTimeMillis();
for (int i = 0; i < 100000; i++)
IsInt_ByCompiledRegex(non_int);
endTime = System.currentTimeMillis();
System.out.print("ByCompiledRegex - non-integer data: ");
System.out.println(endTime - startTime);
startTime = System.currentTimeMillis();
for(int i = 0; i < 100000; i++)
IsInt_ByJonas(big_int);
endTime = System.currentTimeMillis();
System.out.print("\nByJonas - integer data: ");
System.out.println(endTime - startTime);
startTime = System.currentTimeMillis();
for(int i = 0; i < 100000; i++)
IsInt_ByJonas(non_int);
endTime = System.currentTimeMillis();
System.out.print("ByJonas - non-integer data: ");
System.out.println(endTime - startTime);
}
private boolean IsInt_ByException(String str)
{
try
{
Integer.parseInt(str);
return true;
}
catch(NumberFormatException nfe)
{
return false;
}
}
private boolean IsInt_ByRegex(String str)
{
return str.matches("^-?\\d+$");
}
private boolean IsInt_ByCompiledRegex(String str) {
return pattern.matcher(str).find();
}
public boolean IsInt_ByJonas(String str)
{
if (str == null) {
return false;
}
int length = str.length();
if (length == 0) {
return false;
}
int i = 0;
if (str.charAt(0) == '-') {
if (length == 1) {
return false;
}
i = 1;
}
for (; i < length; i++) {
char c = str.charAt(i);
if (c <= '/' || c >= ':') {
return false;
}
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
结果:
ByException - integer data: 45
ByException - non-integer data: 465
ByRegex - integer data: 272
ByRegex - non-integer data: 131
ByCompiledRegex - integer data: 45
ByCompiledRegex - non-integer data: 26
ByJonas - integer data: 8
ByJonas - non-integer data: 2
Run Code Online (Sandbox Code Playgroud)
Łuk*_*nik 32
org.apache.commons.lang.StringUtils.isNumeric
Run Code Online (Sandbox Code Playgroud)
虽然Java的标准库确实错过了这样的实用功能
我认为Apache Commons对每个Java程序员来说都是"必备"
太糟糕了,它还没有移植到Java5
Jon*_*eet 22
它部分取决于你的意思"可以转换为整数".
如果你的意思是"可以转换为Java中的int",那么Jonas的答案是一个良好的开端,但还没有完成这项工作.例如,它将通过99999999999999999999999999999.我会在方法结束时从你自己的问题中添加正常的try/catch调用.
逐字符检查将有效地拒绝"根本不是一个整数"的情况,留下"它是一个整数,但Java无法处理它"的情况被慢速异常路由捕获.你可以做到这一点的手了,但是这将是一个很多更复杂.
Ras*_*ara 16
关于正则表达式的一个评论.这里提供的每个例子都是错的!如果你想使用正则表达式,不要忘记编译模式需要花费很多时间.这个:
str.matches("^-?\\d+$")
Run Code Online (Sandbox Code Playgroud)
还有这个:
Pattern.matches("-?\\d+", input);
Run Code Online (Sandbox Code Playgroud)
导致在每个方法调用中编译模式.要正确使用它,请遵循:
import java.util.regex.Pattern;
/**
* @author Rastislav Komara
*/
public class NaturalNumberChecker {
public static final Pattern PATTERN = Pattern.compile("^\\d+$");
boolean isNaturalNumber(CharSequence input) {
return input != null && PATTERN.matcher(input).matches();
}
}
Run Code Online (Sandbox Code Playgroud)
Bil*_*ard 12
我从rally25rs回复中复制了代码,并为非整数数据添加了一些测试.无可否认,结果有利于Jonas Klemming发布的方法.当你有整数数据时,我最初发布的Exception方法的结果非常好,但是当你没有整数数据时它们是最差的,而RegEx解决方案的结果(我敢打赌很多人使用)是一贯不好.有关编译的正则表达式示例,请参阅Felipe的答案,该示例要快得多.
public void runTests()
{
String big_int = "1234567890";
String non_int = "1234XY7890";
long startTime = System.currentTimeMillis();
for(int i = 0; i < 100000; i++)
IsInt_ByException(big_int);
long endTime = System.currentTimeMillis();
System.out.print("ByException - integer data: ");
System.out.println(endTime - startTime);
startTime = System.currentTimeMillis();
for(int i = 0; i < 100000; i++)
IsInt_ByException(non_int);
endTime = System.currentTimeMillis();
System.out.print("ByException - non-integer data: ");
System.out.println(endTime - startTime);
startTime = System.currentTimeMillis();
for(int i = 0; i < 100000; i++)
IsInt_ByRegex(big_int);
endTime = System.currentTimeMillis();
System.out.print("\nByRegex - integer data: ");
System.out.println(endTime - startTime);
startTime = System.currentTimeMillis();
for(int i = 0; i < 100000; i++)
IsInt_ByRegex(non_int);
endTime = System.currentTimeMillis();
System.out.print("ByRegex - non-integer data: ");
System.out.println(endTime - startTime);
startTime = System.currentTimeMillis();
for(int i = 0; i < 100000; i++)
IsInt_ByJonas(big_int);
endTime = System.currentTimeMillis();
System.out.print("\nByJonas - integer data: ");
System.out.println(endTime - startTime);
startTime = System.currentTimeMillis();
for(int i = 0; i < 100000; i++)
IsInt_ByJonas(non_int);
endTime = System.currentTimeMillis();
System.out.print("ByJonas - non-integer data: ");
System.out.println(endTime - startTime);
}
private boolean IsInt_ByException(String str)
{
try
{
Integer.parseInt(str);
return true;
}
catch(NumberFormatException nfe)
{
return false;
}
}
private boolean IsInt_ByRegex(String str)
{
return str.matches("^-?\\d+$");
}
public boolean IsInt_ByJonas(String str)
{
if (str == null) {
return false;
}
int length = str.length();
if (length == 0) {
return false;
}
int i = 0;
if (str.charAt(0) == '-') {
if (length == 1) {
return false;
}
i = 1;
}
for (; i < length; i++) {
char c = str.charAt(i);
if (c <= '/' || c >= ':') {
return false;
}
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
结果:
ByException - integer data: 47
ByException - non-integer data: 547
ByRegex - integer data: 390
ByRegex - non-integer data: 313
ByJonas - integer data: 0
ByJonas - non-integer data: 16
Run Code Online (Sandbox Code Playgroud)
有番石榴版:
import com.google.common.primitives.Ints;
Integer intValue = Ints.tryParse(stringValue);
Run Code Online (Sandbox Code Playgroud)
如果它无法解析字符串,它将返回null而不是抛出异常.
这个更短,但更短并不一定更好(并且它不会捕获超出范围的整数值,如danatel的评论中所指出的):
input.matches("^-?\\d+$");
Run Code Online (Sandbox Code Playgroud)
就个人而言,由于实现是通过辅助方法进行的,并且正确性胜过长度,我只会选择你所拥有的东西(减去捕获基Exception
类而不是NumberFormatException
).
小智 6
您可以使用字符串类的matches方法.[0-9]表示它可以是的所有值,+表示它必须至少有一个字符长,而*表示它可以是零个或多个字符长.
boolean isNumeric = yourString.matches("[0-9]+"); // 1 or more characters long, numbers only
boolean isNumeric = yourString.matches("[0-9]*"); // 0 or more characters long, numbers only
Run Code Online (Sandbox Code Playgroud)