java 判断字符串是否是整数_检查字符串是否表示Java中的整数的最佳方法是什么?...

因为人们可能还会访问这里,并且在基准测试之后会对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 

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 

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 

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 

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 

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 

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 

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 

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 

char c = str.charAt(i);

if (c <= '/' || c >= ':') {

return false;

}

}

return true;}

结果:ByException - integer data: 45ByException - non-integer data: 465ByRegex - integer data: 272ByRegex - non-integer data: 131ByCompiledRegex - integer data: 45ByCompiledRegex - non-integer data: 26ByJonas - integer data: 8ByJonas - non-integer data: 2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值