<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、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # Kotlin 程序:檢查字符串是否為數字 > 原文: [https://www.programiz.com/kotlin-programming/examples/check-string-numeric](https://www.programiz.com/kotlin-programming/examples/check-string-numeric) #### 在此程序中,您將學習不同的技術來檢查 Kotlin 中的字符串是否為數字。 ## 示例 1:檢查字符串是否為數字 ```kt import java.lang.Double.parseDouble fun main(args: Array<String>) { val string = "12345s15" var numeric = true try { val num = parseDouble(string) } catch (e: NumberFormatException) { numeric = false } if (numeric) println("$string is a number") else println("$string is not a number") } ``` 運行該程序時,輸出為: ```kt 12345s15 is not a number ``` 在上面的程序中,我們有一個名為`string`的`String`,其中包含要檢查的字符串。 我們還有一個布爾值`num`,它存儲最終結果是否為數字。 要檢查`string`是否僅包含數字,在`try`塊中,我們使用`Double`的`parseDouble()`方法將字符串轉換為`Double`。 如果拋出錯誤(即`NumberFormatException`錯誤),則表示`string`不是數字,并且`num`設置為`false`。 否則,這是一個數字。 但是,如果要檢查是否有一定數量的字符串,則需要將其更改為函數。 而且,邏輯基于拋出異常,這可能會非常昂貴。 相反,我們可以使用正則表達式的功能來檢查字符串是否為數字,如下所示。 * * * ## 示例 2:使用正則表達式(regex)檢查字符串是否為數字 ```kt fun main(args: Array<String>) { val string = "-1234.15" var numeric = true numeric = string.matches("-?\\d+(\\.\\d+)?".toRegex()) if (numeric) println("$string is a number") else println("$string is not a number") } ``` 運行該程序時,輸出為: ```kt -1234.15 is a number ``` 在上面的程序中,我們使用正則表達式來檢查`string`是否為數字,而不是使用`try-catch`塊。 這是使用`String`的`matches()`方法完成的。 在`matches()`方法中, * `-?`允許零或多個`-`用于字符串中的負數。 * `\\d+`檢查字符串必須至少包含一個或多個數字(`\\d`)。 * `(\\.\\d+)?`允許零個或多個給定模式`(\\.\\d+)`,其中 * `\\.`檢查字符串是否包含`.`(小數點) * 如果是,則應至少跟一個或多個數字`\\d+`。 以下是等效的 Java 代碼:[檢查字符串是否為數字的 Java 程序](/java-programming/examples/check-string-numeric "Java program to check if a string is numeric or not")。
                  <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>

                              哎呀哎呀视频在线观看