检查String是否表示Java中的整数的最佳方法是什么?

Bil*_*ard 203 java string int

我通常使用以下习惯来检查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)

  • (c <='/'|| c> =':')看起来有点奇怪.我会用(c <'0'|| c>'9')... Java中的<=和> =运算符更快吗? (50认同)
  • 我会在正则表达式之前使用这个方法或原始方法.这对于性能而言,是实现速度和纯粹可维护性的原始方法.正则表达式解决方案没有任何意义. (15认同)
  • 我担心溢出,但这种方法可以适用于BigInts,并且仍然比其他方法更快.如果有人想知道我为什么要在这么简单的问题上投入太多精力,我正在创建一个库来帮助解决Project Euler问题. (4认同)
  • 为什么不使用正则表达式?不是返回str.matches("^ - ?\\ d + $")与上面的代码相同. (3认同)
  • 如果您担心是否可以将字符串实际解析为 int 或 long,您还需要检查字符串表示的整数是否确实适合这些数据类型。 (2认同)

Ovi*_*rar 60

你拥有它,但你应该只抓住它NumberFormatException.

  • 是的,抓住比你需要的更多例外被认为是不好的形式. (6认同)

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的解决方案也是最强大的.看起来他赢了:)

  • 这三个基准测试的好主意.为了公平对待Regex和Jonas方法,你应该使用非整数字符串进行测试,因为这是Integer.parseInt方法真正减慢的地方. (13认同)
  • 对不起,但这个正则表达式测试不好.(1)你不需要第二次对`^`和`$`进行正则表达式引擎检查,因为在`matches`中,整个字符串必须匹配正则表达式,(2)`str.matches`每次都必须创建自己的`模式`这是昂贵的.出于性能原因,我们应该只在此方法之外创建一次这样的Pattern并在其中使用它.(3)我们也可以只创建一个Matcher对象并使用它的`reset(CharSequence)`来传递用户数据并返回它的`matches()`结果. (4认同)

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)

  • @MartinCarney我修改了它并对基准模式编译进行了基准测试.显然我的CPU/JIT更快,但如果我将其插回,则编译时间为"336". (2认同)
  • 要清楚,336(ms)是模式编译完成100k次后发生的情况,就像所有其他行一样.暗示它只做了一次,它的时间基本上为零. (2认同)

Łuk*_*nik 32

org.apache.commons.lang.StringUtils.isNumeric 
Run Code Online (Sandbox Code Playgroud)

虽然Java的标准库确实错过了这样的实用功能

我认为Apache Commons对每个Java程序员来说都是"必备"

太糟糕了,它还没有移植到Java5

  • 另一个问题是负数,但我也+1,因为我认为这种方法最接近一个好的解决方案。 (2认同)

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)

  • 您可以通过提前创建Matcher并使用其reset()方法将其应用于输入来挤出更多性能. (5认同)

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)

  • 谢谢你收拾我的懈怠!:) (2认同)

aba*_*rek 9

有番石榴版:

import com.google.common.primitives.Ints;

Integer intValue = Ints.tryParse(stringValue);
Run Code Online (Sandbox Code Playgroud)

如果它无法解析字符串,它将返回null而不是抛出异常.

  • 最佳答案恕我直言.使用经过良好测试的库而不是汇总您自己的解决方案.(另请参阅讨论[此处](https://github.com/google/guava/issues/1839).) (2认同)

Jon*_*nan 6

这个更短,但更短并不一定更好(并且它不会捕获超出范围的整数值,如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)