Rumah> Java> javaTutorial> teks badan

Perbandingan Rentetan di Jawa

PHPz
Lepaskan: 2024-08-30 16:27:55
asal
971 orang telah melayarinya

In Java, a sequence of characters is known as a string. It is immutable (cannot be changed once it is created) and helps in performing several operations. Also, a Comparison of String is a common programming task in Java. It can be performed using several ways, and it will be discussed in the following sections in detail.

How to Compare String in Java?

As already discussed, a String comparison can be done using different methods. They are:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

  • Using equals() method
  • Using compareTo() method
  • Using equalsIgnoreCase() method
  • Using compareToIgnoreCase() method

1. Equals() Method

equals() method compares two strings based on the string content. If the strings are not in the same case (i.e. upper or lower case), it will be considered as not equal. Below is an example of the equals() method.

Program

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"); } } }
Salin selepas log masuk

Output:

Perbandingan Rentetan di Jawa

Explanation of the above code

  • Here, two strings str1 and str2, are equal as they are of the same case. (Camel case).
  • Also, Str3 and Str4, str5 and str6 are not equal.

2. Compare to() Method

In this method, values are compared lexicographically and return a value of integer type. The value is based on whether the first string is equal to, less than or greater than the 2ndstring.

Two strings str1 and str2 are present and

  • if str1 == str2 , then 0
  • if str1 > str2 , then +ve value
  • if str1 < str2 , then -ve value

Program

//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)); } }
Salin selepas log masuk

Output:

Perbandingan Rentetan di Jawa

Explanation of the above code

  • Since two strings str1 and str2 are equal lexicographically, 0 is returned.
  • At the same time, str3 is less than str4, and str5 is less than str6 lexicographically. So, a negative value is returned.

3. Equals Ignore Case Method

Using this method, two strings will be compared without considering whether the string is upper case or lower case.

Program

//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)); } }
Salin selepas log masuk

Output:

Perbandingan Rentetan di Jawa

Explanation of the above code

  • Since the equalsIgnoreCase method is used, str1 and str2 are equal even though they are of different cases(small case and upper case). So, True is returned.
  • In the case of str3 and str4, str5 and str6, the two strings are different. Hence, false is returned.

4. Compare to Ignore Case Method

This method is similar to the compareTo method, where strings are compared lexicographically. The difference is that comparison won’t be affected whether the strings are in upper case or lower case.

Program

//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)); } }
Salin selepas log masuk

Output:

Perbandingan Rentetan di Jawa

Explanation of the above code

  • Here, a combination of compareTo and Ignorecase is done. Since both the str1 and str2 are equal without considering the cases, 0 is returned.
  • Similarly, a positive value is returned as str3 is greater than str4, and a negative value is returned as str5 is less than str6.

Now, let us see all the above-mentioned methods in a single program to better understand string comparison.

Program

//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)); } }
Salin selepas log masuk

Output:

Perbandingan Rentetan di Jawa

Conclusion

A string is a sequence of characters, and its objects are immutable. There are different methods such as equals, compareTo, etc., available in order to compare the strings. All these methods are used based on the requirements. They are explained in the above section in detail.

Atas ialah kandungan terperinci Perbandingan Rentetan di Jawa. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!