比较 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 次浏览

开启你的事业

完成课程即可获得认证

立刻开始
广告
© . All rights reserved.