<bdo id='VkUeZ'></bdo><ul id='VkUeZ'></ul>
    <tfoot id='VkUeZ'></tfoot>

  1. <legend id='VkUeZ'><style id='VkUeZ'><dir id='VkUeZ'><q id='VkUeZ'></q></dir></style></legend>

  2. <small id='VkUeZ'></small><noframes id='VkUeZ'>

    <i id='VkUeZ'><tr id='VkUeZ'><dt id='VkUeZ'><q id='VkUeZ'><span id='VkUeZ'><b id='VkUeZ'><form id='VkUeZ'><ins id='VkUeZ'></ins><ul id='VkUeZ'></ul><sub id='VkUeZ'></sub></form><legend id='VkUeZ'></legend><bdo id='VkUeZ'><pre id='VkUeZ'><center id='VkUeZ'></center></pre></bdo></b><th id='VkUeZ'></th></span></q></dt></tr></i><div id='VkUeZ'><tfoot id='VkUeZ'></tfoot><dl id='VkUeZ'><fieldset id='VkUeZ'></fieldset></dl></div>
    1. 检测字符串是否为数字的最优雅方法?

      Most elegant way to detect if a String is a number?(检测字符串是否为数字的最优雅方法?)
    2. <i id='gqFoD'><tr id='gqFoD'><dt id='gqFoD'><q id='gqFoD'><span id='gqFoD'><b id='gqFoD'><form id='gqFoD'><ins id='gqFoD'></ins><ul id='gqFoD'></ul><sub id='gqFoD'></sub></form><legend id='gqFoD'></legend><bdo id='gqFoD'><pre id='gqFoD'><center id='gqFoD'></center></pre></bdo></b><th id='gqFoD'></th></span></q></dt></tr></i><div id='gqFoD'><tfoot id='gqFoD'></tfoot><dl id='gqFoD'><fieldset id='gqFoD'></fieldset></dl></div>

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

        • <bdo id='gqFoD'></bdo><ul id='gqFoD'></ul>

                <tbody id='gqFoD'></tbody>
              <tfoot id='gqFoD'></tfoot>
                <legend id='gqFoD'><style id='gqFoD'><dir id='gqFoD'><q id='gqFoD'></q></dir></style></legend>

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

                问题描述

                有没有比

                boolean isNumber = false;
                try{
                   Double.valueOf(myNumber);
                   isNumber = true;
                } catch (NumberFormatException e) {
                }
                

                ...?

                编辑:由于我无法选择两个答案,我将使用正则表达式一个,因为 a) 它很优雅 b) 说Jon Skeet 解决了问题"是重言式,因为 Jon Skeet 自己是所有问题的解决方案.

                Edit: Since I can't pick two answers I'm going with the regex one because a) it's elegant and b) saying "Jon Skeet solved the problem" is a tautology because Jon Skeet himself is the solution to all problems.

                推荐答案

                我不相信 Java 中内置了任何东西可以更快、更可靠地完成它,假设稍后你会想用 Double 实际解析它.valueOf(或类似).

                I don't believe there's anything built into Java to do it faster and still reliably, assuming that later on you'll want to actually parse it with Double.valueOf (or similar).

                我会使用 Double.parseDouble 而不是 Double.valueOf 来避免不必要地创建 Double,并且您还可以通过检查数字来比异常更快地摆脱明显愚蠢的数字,e/E, - 和 .预先.所以,类似:

                I'd use Double.parseDouble instead of Double.valueOf to avoid creating a Double unnecessarily, and you can also get rid of blatantly silly numbers quicker than the exception will by checking for digits, e/E, - and . beforehand. So, something like:

                public boolean isDouble(String value)
                {        
                    boolean seenDot = false;
                    boolean seenExp = false;
                    boolean justSeenExp = false;
                    boolean seenDigit = false;
                    for (int i=0; i < value.length(); i++)
                    {
                        char c = value.charAt(i);
                        if (c >= '0' && c <= '9')
                        {
                            seenDigit = true;
                            continue;
                        }
                        if ((c == '-' || c=='+') && (i == 0 || justSeenExp))
                        {
                            continue;
                        }
                        if (c == '.' && !seenDot)
                        {
                            seenDot = true;
                            continue;
                        }
                        justSeenExp = false;
                        if ((c == 'e' || c == 'E') && !seenExp)
                        {
                            seenExp = true;
                            justSeenExp = true;
                            continue;
                        }
                        return false;
                    }
                    if (!seenDigit)
                    {
                        return false;
                    }
                    try
                    {
                        Double.parseDouble(value);
                        return true;
                    }
                    catch (NumberFormatException e)
                    {
                        return false;
                    }
                }
                

                请注意,尽管尝试了几次,这仍然不包括NaN"或十六进制值.您是否希望这些通过取决于上下文.

                Note that despite taking a couple of tries, this still doesn't cover "NaN" or hex values. Whether you want those to pass or not depends on context.

                根据我的经验,正则表达式比上面的硬编码检查要慢.

                In my experience regular expressions are slower than the hard-coded check above.

                这篇关于检测字符串是否为数字的最优雅方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                Compiling C++ for the JVM(为 JVM 编译 C++)
                Compile to java bytecode (without using Java)(编译成java字节码(不使用Java))
                How to drive C#, C++ or Java compiler to compute 1+2+3+...+1000 at compile time?(如何在编译时驱动 C#、C++ 或 Java 编译器计算 1+2+3+...+1000?)
                Java ClassLoader: load same class twice(Java ClassLoader:两次加载相同的类)
                How to debug .class files in ECLIPSE?(如何在 ECLIPSE 中调试 .class 文件?)
                Java quot;The blank final field may not have been initializedquot; Anonymous Interface vs Lambda Expression(Java“可能尚未初始化空白的最终字段匿名接口与 Lambda 表达式)

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

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

                    <legend id='bjPbL'><style id='bjPbL'><dir id='bjPbL'><q id='bjPbL'></q></dir></style></legend>
                  • <tfoot id='bjPbL'></tfoot>