Java中的大小寫敏感字串比較。


可以使用equals()方法或compareTo()方法比較兩個字串。

其中,

  • equals()方法將此字串與指定物件進行比較。
  • compareTo()方法按字典順序比較兩個字串。比較基於字串中每個字元的Unicode值。

這兩種方法比較給定字串時,都考慮了大小寫,即大小寫不同的字串將被視為不同的字串。

示例

以下示例演示了使用equals()方法比較兩個字串。

線上演示

public class Sample{
   public static void main(String args[]) {
      String str = "Hello World";
      String anotherString = "hello world";
      Object objStr = str;
      System.out.println( str.equals(anotherString) );
   }
}

輸出

false

更新於: 2020年2月26日

3000+ 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.