日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

使用Java中的equals()和compareTo()方法比较字符串

發布時間:2023/12/1 java 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用Java中的equals()和compareTo()方法比较字符串 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Given strings and we have to compare them using equals() and compareTo() method.

給定字符串,我們必須使用equals()和compareTo()方法進行比較。

  • Java string equals() method

    Java字符串equals()方法

    Java string equals() method compares the content of two strings, If all characters are the same, it returns

    Java字符串equals()方法比較兩個字符串的內容,如果所有字符都相同,則返回

    true, else it returns false.

    是true ,否則返回false 。

  • Java string compareTo() method

    Java字符串compareTo()方法

    Java string compareTo() method is called with a string and another string is supplied as an argument, it compares the strings based on the Unicode values of the characters in the strings. It returns a

    使用字符串調用Java字符串compareTo()方法,并提供另一個字符串作為參數,它根據字符串中字符的Unicode值比較字符串。 它返回一個

    positive number, negative number or 0. If both strings have the same content, it returns 0.

    正數 , 負數或0 。 如果兩個字符串的內容相同,則返回0 。

Java代碼使用equals(),compareTo()和==運算符比較字符串 (Java code to compare the strings using equals(), compareTo() and == operator )

// Comparing Strings with equals() and compareTo() // methods in Javapublic class Main {public static void main(String[] args) {//stringsString str1 = new String("ABC");String str2 = new String("PQR");//comparing strings using equals() methodSystem.out.println(str1.equals(str2));System.out.println(str1.equals(str1));//comparing strings using == operator System.out.println(str1 == str1);System.out.println(str1 == str2);//comparing strings using compareTo() methodSystem.out.println(str1.compareTo(str1));System.out.println(str1.compareTo(str2));} }

Output

輸出量

false true true false 0 -15

翻譯自: https://www.includehelp.com/java-programs/comparing-strings-with-equals-and-compareto-methods-in-java.aspx

總結

以上是生活随笔為你收集整理的使用Java中的equals()和compareTo()方法比较字符串的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。