比較 java 中的字串和字串部分


以下是一個示例,展示瞭如何比較 Java 中的字串和字串的部分內容?請舉例說明。

示例

現場演示

public class StringDemo {
   public static void main(String[] args) {
      String str1 = "tutorials point";
      String str2 = str1.substring(10);
      int result = str1.compareTo(str2);

      // prints the return value of the comparison
      if (result < 0) {
         System.out.println("str1 is greater than str2");
      } else if (result == 0) {
         System.out.println("str1 is equal to str2");
      } else {
         System.out.println("str1 is less than str2");
      }
   }
}

輸出

str1 is less than str2

更新於: 2020 年 2 月 26 日

182 次瀏覽

開啟你的事業

完成課程即可獲得認證

立刻開始
廣告