在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次瀏覽

開啟你的職業生涯

獲得課程認證

開始學習
Advertisement
© . All rights reserved.