在 Java 中,字元序列稱為字串。它是不可變的(一旦創建就無法更改)並有助於執行多種操作。此外,字串比較是 Java 中常見的程式設計任務。它可以透過多種方式執行,並將在以下部分詳細討論。
正如已經討論過的,可以使用不同的方法來完成字串比較。他們是:
開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
equals() 方法根據字串內容比較兩個字串。如果字串的大小寫不同(即大寫或小寫),則將被視為不相等。下面是 equals() 方法的範例。
節目
public class StringComparisonExamples { public static void main(String[] args) { String str1 = "Balloon"; String str2 = "Balloon"; //string comparison using equals method if (str1.equals(str2)) { System.out.println("Both str1 : " + str1 + " and str2 : "+ str2 +" are equal"); } else { System.out.println("Both str1 : " + str1 + " and str2 : "+ str2 +" are not equal"); } String str3 = "Happy"; String str4 = "hai"; if (str3.equals(str4)) { System.out.println("Both str3 : " + str3 + " and str4 : "+ str4 +" are equal"); } else { System.out.println("Both str3 : " + str3 + " and str4 : "+ str4 +" are not equal"); } String str5 = "Happy"; String str6 = "hard"; if (str5.equals(str6)) { System.out.println("Both str5 : " + str5 + "and str6 : "+ str6 +" are equal"); } else { System.out.println("Both str5 : " + str5 + " and str6 : "+ str6 +" are not equal"); } } }
輸出:
上面程式碼的解釋
在此方法中,按字典順序比較值並傳回整數類型的值。該值是基於第一個字串是否等於、小於或大於第 2 個nd 字串。
存在兩個字串 str1 和 str2 且
節目
//Java program to demonstrate compareTo method public class StringComparisonExamples { public static void main(String[] args) { String str1 = "Balloon"; String str2 = "Balloon"; String str3 = "Happy"; String str4 = "hai"; //string comparison using compareTo method System.out.println(str1.compareTo(str2)); System.out.println(str3.compareTo(str4)); String str5 = "Happy"; String str6 = "Hardest one"; System.out.println(str5.compareTo(str6)); } }
輸出:
上面程式碼的解釋
使用此方法,將比較兩個字串,而不考慮字串是大寫還是小寫。
節目
//Java program to demonstrate equalsIgnoreCase method public class StringComparisonExamples { public static void main(String[] args) { String str1 = "Balloon"; String str2 = "balloon"; //string comparison using equalsIgnoreCase method System.out.println(str1.equalsIgnoreCase(str2)); String str3 = "Happy"; String str4 = "hai"; System.out.println(str3.equalsIgnoreCase(str4)); String str5 = "Happy"; String str6 = "hard"; System.out.println(str5.equalsIgnoreCase(str6)); } }
輸出:
上面程式碼的解釋
此方法類似compareTo方法,依字典順序比較字串。不同的是,無論字串是大寫還是小寫,都不會影響比較。
節目
//Java program to demonstrate compareToIgnoreCase method public class StringComparisonExamples { public static void main(String[] args) { String str1 = "Balloon"; String str2 = "balloon"; //string comparison using compareToIgnoreCase method System.out.println(str1.compareToIgnoreCase(str2)); String str3 = "Happy"; String str4 = "hai"; System.out.println(str3.compareToIgnoreCase(str4)); String str5 = "Happy"; String str6 = "Hard"; System.out.println(str5.compareToIgnoreCase(str6)); } }
輸出:
上面程式碼的解釋
現在,讓我們在一個程式中查看所有上述方法,以便更好地理解字串比較。
節目
//Java program to demonstrate different methods for string comparison public class StringComparisonExamples { public static void main(String[] args) { String str1 = "Balloon"; String str2 = "Balloon"; System.out.println("Comparison of str1 : " + str1 + " and str2 : " + str2); System.out.println("Using equals method "); //string comparison using equals method if (str1.equals(str2)) { System.out.println("Both are equal"); } else { System.out.println("Both are not equal"); } System.out.println("Using compareTo method "); //string comparison using compareTo method System.out.println(str1.compareTo(str2)); System.out.println("Using equalsIgnoreCase method "); //string comparison using equalsIgnoreCase method System.out.println(str1.equalsIgnoreCase(str2)); System.out.println("Using compareToIgnoreCase method "); //string comparison using compareToIgnoreCase method System.out.println(str1.compareToIgnoreCase(str2)); String str3 = "Happy"; String str4 = "hai"; System.out.println("\nComparison of str3 : " + str3 + " and str4 : " + str4); System.out.println("Using equals method "); if (str3.equals(str4)) { System.out.println("Both are equal"); } else { System.out.println("Both are not equal"); } System.out.println("Using compareTo method "); System.out.println(str3.compareTo(str4)); System.out.println("Using equalsIgnoreCase method "); System.out.println(str3.equalsIgnoreCase(str4)); System.out.println("Using compareToIgnoreCase method "); System.out.println(str3.compareToIgnoreCase(str4)); String str5 = "Happy"; String str6 = "hard"; System.out.println("\nComparison of str5 : " + str5 + " and str6 : " + str6); System.out.println("Using equals method "); if (str5.equals(str6)) { System.out.println("Both are equal"); } else { System.out.println("Both are not equal"); } System.out.println("Using compareTo method "); System.out.println(str5.compareTo(str6)); System.out.println("Using equalsIgnoreCase method "); System.out.println(str5.equalsIgnoreCase(str6)); System.out.println("Using compareToIgnoreCase method "); System.out.println(str5.compareToIgnoreCase(str6)); } }
輸出:
字串是字元序列,其物件是不可變的。有不同的方法可用於比較字串,例如 equals、compareTo 等。所有這些方法都是根據要求使用的。它們在上一節中有詳細解釋。
以上是Java 中的字串比較的詳細內容。更多資訊請關注PHP中文網其他相關文章!