<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>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Kotlin 程序:比較字符串 > 原文: [https://www.programiz.com/kotlin-programming/examples/compare-strings](https://www.programiz.com/kotlin-programming/examples/compare-strings) #### 在此程序中,您將學習在 Kotlin 中比較兩個字符串。 ## 示例 1:比較兩個字符串 ```kt fun main(args: Array<String>) { val style = "Bold" val style2 = "Bold" if (style == style2) println("Equal") else println("Not Equal") } ``` 運行該程序時,輸出為: ```kt Equal ``` 在上面的程序中,我們有兩個字符串`style`和`style2`。 我們僅使用相等運算符(`==`)比較兩個字符串,這會將值`Bold`與`Bold`進行比較,并輸出`Equal`。 * * * ## 示例 2:使用`equals()`比較兩個字符串 ```kt fun main(args: Array<String>) { val style = "Bold" val style2 = "Bold" if (style.equals(style2)) println("Equal") else println("Not Equal") } ``` 運行該程序時,輸出為: ```kt Equal ``` 在上面的程序中,我們有兩個字符串`style`和`style2`都包含相同的單詞`Bold`。 如您所見,我們已經使用`equals()`方法比較字符串。 與示例 1 相似,它將值`Bold`與`Bold`進行比較。 * * * ## 示例 3:使用`===`比較兩個字符串(不起作用) ```kt fun main(args: Array<String>) { val style = buildString { "Bold" } val style2 = buildString { "Bold" } if (style === style2) println("Equal") else println("Not Equal") } ``` 運行該程序時,輸出為: ```kt Not Equal ``` 在上面的程序中,我們使用輔助方法`buildString`創建了`String`對象,而不是僅使用引號創建了字符串。 我們沒有使用`==`運算符,而是使用了`===`(引用相等運算符)來比較字符串。 該運算符比較`style`和`style2`本質上是否是同一對象。 由于它們不是,因此`Not Equal`被打印在屏幕上。 * * * ## 示例 4:比較兩個字符串的不同方法 這是在 Java 中可能進行的字符串比較。 ```kt fun main(args: Array<String>) { val style = buildString { "Bold" } val style2 = buildString { "Bold" } var result = style.equals("Bold") // true println(result) result = style2 === "Bold" // false println(result) result = style === style2 // false println(result) result = "Bold" === "Bold" // true println(result) } ``` 運行該程序時,輸出為: ```kt true false false true ``` 以下是等效的 Java 代碼:[比較字符串的 Java 程序](/java-programming/examples/compare-strings "Java Program to compare strings")。
                  <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>

                              哎呀哎呀视频在线观看