<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # Java 程序:檢查字符串是否為數字 > 原文: [https://www.programiz.com/java-programming/examples/check-string-numeric](https://www.programiz.com/java-programming/examples/check-string-numeric) #### 在此程序中,您將學習不同的技術來檢查 Java 中字符串是否為數字。 ## 示例 1:檢查字符串是否為數字 ```java public class Numeric { public static void main(String[] args) { String string = "12345.15"; boolean numeric = true; try { Double num = Double.parseDouble(string); } catch (NumberFormatException e) { numeric = false; } if(numeric) System.out.println(string + " is a number"); else System.out.println(string + " is not a number"); } } ``` 運行該程序時,輸出為: ```java 12345.15 is a number ``` 在上面的程序中,我們有一個名為`string`的`String`,其中包含要檢查的字符串。 我們還有一個布爾值`number`,它存儲最終結果是否為數字。 要檢查`string`是否僅包含數字,在`try`塊中,我們使用`Double`的`parseDouble()`方法將字符串轉換為`Double`。 如果拋出錯誤(即`NumberFormatException`錯誤),則表示`string`不是數字,并且`number`設置為`false`。 否則,這是一個數字。 但是,如果要檢查是否有多個字符串,則需要將其更改為函數。 而且,邏輯基于拋出異常,這可能會非常昂貴。 相反,我們可以使用正則表達式的功能來檢查字符串是否為數字,如下所示。 * * * ## 示例 2:使用正則表達式(regex)檢查字符串是否為數字 ```java public class Numeric { public static void main(String[] args) { String string = "-1234.15"; boolean numeric = true; numeric = string.matches("-?\\d+(\\.\\d+)?"); if(numeric) System.out.println(string + " is a number"); else System.out.println(string + " is not a number"); } } ``` 運行該程序時,輸出為: ```java -1234.15 is a number ``` 在上面的程序中,我們使用正則表達式來檢查`string`是否為數字,而不是使用`try-catch`塊。 這是使用`String`的`matches()`方法完成的。 在`matches()`方法中, * `-?`允許零或多個`-`用于字符串中的負數。 * `\\d+`檢查字符串必須至少包含 1 個或多個數字(`\\d`)。 * `(\\.\\d+)?`允許零個或多個給定模式`(\\.\\d+)`,其中 * `\\.`檢查字符串是否包含`.`(小數點) * 如果是,則應至少跟一個或多個數字`\\d+`。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看