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

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

      • <bdo id='kwGhV'></bdo><ul id='kwGhV'></ul>
      <tfoot id='kwGhV'></tfoot>
    1. <legend id='kwGhV'><style id='kwGhV'><dir id='kwGhV'><q id='kwGhV'></q></dir></style></legend>
    2. 用于检查字符串是否包含*无*异常的数字的 Java

      时间:2023-09-29
      <legend id='DI4jM'><style id='DI4jM'><dir id='DI4jM'><q id='DI4jM'></q></dir></style></legend>

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

            <bdo id='DI4jM'></bdo><ul id='DI4jM'></ul>

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

              • 本文介绍了用于检查字符串是否包含*无*异常的数字的 Java 库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                如果传递的字符串是有效数字(例如123.55e-9"、-333,556"),我正在寻找一种返回布尔值的方法.我不想只想做:

                I'm looking for a method that returns a boolean if the String it is passed is a valid number (e.g. "123.55e-9", "-333,556"). I don't want to just do:

                public boolean isANumber(String s) {
                    try { 
                        BigDecimal a = new BigDecimal(s); 
                        return true;
                    } catch (NumberFormatException e) {
                        return false;
                    }
                }
                

                显然,该函数应该使用状态机 (DFA) 来解析字符串,以确保无效示例不会欺骗它(例如-21,22.22.2"、33-2").你知道是否存在这样的图书馆吗?我真的不想自己写它,因为这是一个显而易见的问题,我敢肯定我会重新发明轮子.

                Clearly, the function should use a state machine (DFA) to parse the string to make sure invalid examples don't fool it (e.g. "-21,22.22.2", "33-2"). Do you know if any such library exists? I don't really want to write it myself as it's such an obvious problem that I'm sure I'd be re-inventing the wheel.

                谢谢,

                尼克

                推荐答案

                我会避免重新发明这种方法并使用 Apache Commons.如果您使用 Spring、Struts 或许多其他常用的 java 库,它们通常包含 Apache commons.您将需要 commons-lang.jar 文件.这是 NumberUtils 你想要的:

                I would avoid re-inventing this method and go with Apache Commons. If your using Spring, Struts or many other commonly used java libraries, they often have Apache commons included. You will want the commons-lang.jar file. Here is the method in NumberUtils you would want:

                isNumber[1]
                
                public static boolean isNumber(java.lang.String str)
                Checks whether the String a valid Java number.
                
                Valid numbers include hexadecimal marked with the 0x qualifier, scientific notation and numbers marked with a type qualifier (e.g. 123L).
                
                Null and empty String will return false.
                
                Parameters:
                str - the String to check
                Returns:
                true if the string is a correctly formatted number
                

                这篇关于用于检查字符串是否包含*无*异常的数字的 Java 库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:防止 Java 中 String.format("%.2f", doubleValue) 的 下一篇:JSONObject 删除空值对

                相关文章

                最新文章

                  <bdo id='svZ33'></bdo><ul id='svZ33'></ul>

                1. <small id='svZ33'></small><noframes id='svZ33'>

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

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