<legend id='MPLpA'><style id='MPLpA'><dir id='MPLpA'><q id='MPLpA'></q></dir></style></legend>

  1. <tfoot id='MPLpA'></tfoot>
    <i id='MPLpA'><tr id='MPLpA'><dt id='MPLpA'><q id='MPLpA'><span id='MPLpA'><b id='MPLpA'><form id='MPLpA'><ins id='MPLpA'></ins><ul id='MPLpA'></ul><sub id='MPLpA'></sub></form><legend id='MPLpA'></legend><bdo id='MPLpA'><pre id='MPLpA'><center id='MPLpA'></center></pre></bdo></b><th id='MPLpA'></th></span></q></dt></tr></i><div id='MPLpA'><tfoot id='MPLpA'></tfoot><dl id='MPLpA'><fieldset id='MPLpA'></fieldset></dl></div>

      • <bdo id='MPLpA'></bdo><ul id='MPLpA'></ul>
    1. <small id='MPLpA'></small><noframes id='MPLpA'>

    2. 检查字符串是否代表Java中的整数的最佳方法是什么?

      What#39;s the best way to check if a String represents an integer in Java?(检查字符串是否代表Java中的整数的最佳方法是什么?)

      <legend id='JTrps'><style id='JTrps'><dir id='JTrps'><q id='JTrps'></q></dir></style></legend>

        <tfoot id='JTrps'></tfoot>
        • <small id='JTrps'></small><noframes id='JTrps'>

              <bdo id='JTrps'></bdo><ul id='JTrps'></ul>
                <tbody id='JTrps'></tbody>
            • <i id='JTrps'><tr id='JTrps'><dt id='JTrps'><q id='JTrps'><span id='JTrps'><b id='JTrps'><form id='JTrps'><ins id='JTrps'></ins><ul id='JTrps'></ul><sub id='JTrps'></sub></form><legend id='JTrps'></legend><bdo id='JTrps'><pre id='JTrps'><center id='JTrps'></center></pre></bdo></b><th id='JTrps'></th></span></q></dt></tr></i><div id='JTrps'><tfoot id='JTrps'></tfoot><dl id='JTrps'><fieldset id='JTrps'></fieldset></dl></div>

                本文介绍了检查字符串是否代表Java中的整数的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我通常使用以下成语来检查字符串是否可以转换为整数.

                I normally use the following idiom to check if a String can be converted to an integer.

                public boolean isInteger( String input ) {
                    try {
                        Integer.parseInt( input );
                        return true;
                    }
                    catch( Exception e ) {
                        return false;
                    }
                }
                

                只有我一个人,还是这看起来有点骇人听闻?有什么更好的方法?

                Is it just me, or does this seem a bit hackish? What's a better way?

                查看我的答案(带有基准,基于 早期答案.com/users/28278/codingwithspike">CodingWithSpike) 了解我为什么改变立场并接受 Jonas Klemming 的回答 这个问题.我认为这个原始代码会被大多数人使用,因为它实现起来更快,更易于维护,但在提供非整数数据时速度会慢几个数量级.

                See my answer (with benchmarks, based on the earlier answer by CodingWithSpike) to see why I've reversed my position and accepted Jonas Klemming's answer to this problem. I think this original code will be used by most people because it's quicker to implement, and more maintainable, but it's orders of magnitude slower when non-integer data is provided.

                推荐答案

                如果您不关心潜在的溢出问题,此函数的执行速度将比使用 Integer.parseInt() 快 20-30 倍.

                If you are not concerned with potential overflow problems this function will perform about 20-30 times faster than using 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;
                }
                

                这篇关于检查字符串是否代表Java中的整数的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                How can I detect integer overflow on 32 bits int?(如何检测 32 位 int 上的整数溢出?)
                Local variables before return statements, does it matter?(return 语句之前的局部变量,这有关系吗?)
                How to convert Integer to int?(如何将整数转换为整数?)
                How do I create an int array with randomly shuffled numbers in a given range(如何在给定范围内创建一个随机打乱数字的 int 数组)
                Inconsistent behavior on java#39;s ==(java的行为不一致==)
                Why is Java able to store 0xff000000 as an int?(为什么 Java 能够将 0xff000000 存储为 int?)

                      <tbody id='QvjFT'></tbody>

                    • <legend id='QvjFT'><style id='QvjFT'><dir id='QvjFT'><q id='QvjFT'></q></dir></style></legend>

                        <small id='QvjFT'></small><noframes id='QvjFT'>

                      1. <tfoot id='QvjFT'></tfoot>

                        • <bdo id='QvjFT'></bdo><ul id='QvjFT'></ul>
                          <i id='QvjFT'><tr id='QvjFT'><dt id='QvjFT'><q id='QvjFT'><span id='QvjFT'><b id='QvjFT'><form id='QvjFT'><ins id='QvjFT'></ins><ul id='QvjFT'></ul><sub id='QvjFT'></sub></form><legend id='QvjFT'></legend><bdo id='QvjFT'><pre id='QvjFT'><center id='QvjFT'></center></pre></bdo></b><th id='QvjFT'></th></span></q></dt></tr></i><div id='QvjFT'><tfoot id='QvjFT'></tfoot><dl id='QvjFT'><fieldset id='QvjFT'></fieldset></dl></div>