![]() |
坚强的煎饼 · USAJOBS - Search· 1 月前 · |
![]() |
英勇无比的金鱼 · 小红书同款手机挂绳水晶爱心串珠手链时尚ins ...· 2 月前 · |
![]() |
风流倜傥的凳子 · 螺旋“光钥”解码全息“锁” ...· 2 月前 · |
![]() |
傻傻的荒野 · SVG转JPG – 在线将SVG文档转换成至JPG· 4 月前 · |
![]() |
沉着的毛衣
7 月前 |
Method 1: using == operator
Double equals operator is used to compare two or more than two objects, If they are referring to the same object then return true, otherwise return false. String is immutable in java. When two or more objects are created without new keyword, then both object refer same value. Double equals operator actually compares objects references.
The below example illustrates the use of == for string comparison in Java:
// Compare s1 and s2
// It should return true as they both
// refer to same object in memory
System.out.println(s1 +
" == "
+ s2 +
": "
+ (s1 == s2));
// Compare s1 and s3
// It should return true as they both
// refer to same object in memory
System.out.println(s1 +
" == "
+ s3 +
": "
+ (s1 == s3));
// Compare s2 and s3
// It should return true as they both
// refer to same object in memory
System.out.println(s2 +
" == "
+ s3 +
": "
+ (s2 == s3));
// Compare s1 and s4
// It should return false as they both
// refer to different object in memory
System.out.println(s1 +
" == "
+ s4 +
": "
+ (s1 == s4));
Method 2: Using equals() method
In Java, string equals() method compares the two given strings based on the data / content of the string. If all the contents of both the strings are same then it returns true. If all characters are not matched then it returns false.
Below example illustrate the use of .equals for string comparison in Java:
// It should return true as they both
// have the same content
System.out.println(s1 +
" .equals "
+ s2
+
": "
+ s1.equals(s2));
// Compare s1 and s3
// It should return false as they both
// have the different content
System.out.println(s1 +
" .equals "
+ s3
+
": "
+ s1.equals(s3));
// Compare s2 and s3
// It should return false as they both
// have the different content
System.out.println(s2 +
" .equals "
+ s3
+
": "
+ s2.equals(s3));
// Compare s1 and s4
// It should return true as they both
// have the same content
System.out.println(s1 +
" .equals "
+ s4
+
": "
+ s1.equals(s4));
Method 3: Using compareTo() method
In java, Comparable interface compares values and returns an int, these int values may be less than, equal, or greater than 0. The java compares two strings based on the Unicode value of each character in the strings. If two strings are different, then they have different characters at some index that is a valid index for both strings, or their lengths are different, or both.
Assuming index ‘i’ is where characters are different then compareTo() will return firstString.charAt(i)-secondString.charAt(i) .
Below example illustrate the use of .compareTo for string comparison in Java:
// It should return 0 as they both
// have the same ASCII value
System.out.println(s1 +
" .compareTo "
+ s2
+
": "
+ s1.compareTo(s2));
// Compare s1 and s3
// It should return -32 as they both
// have the different ASCII value
System.out.println(s1 +
" .compareTo "
+ s3
+
": "
+ s1.compareTo(s3));
// Compare s3 and s2
// It should return 32 as they both
// have the different ASCII value
System.out.println(s3 +
" .compareTo "
+ s2
+
": "
+ s3.compareTo(s2));
// Compare s1 and s4
// It should return 0 as they both
// have the same ASCII value
System.out.println(s1 +
" .compareTo "
+ s4
+
": "
+ s1.compareTo(s4));
Method 4: Using equalsIgnoreCase() method
Java String equalsIgnoreCase() method is much similar to equals() method, except that case is ignored like in above example String object s4 compare to s3 then equals() method return false, but here in case of equalsIgnoreCase() it will return true. Hence equalsIgnoreCase() method is Case Insensitive.
Below example illustrate the use of .equalsIgnoreCase for string comparison in Java:
// It should return true as they both
// have the same content
System.out.println(s1 +
" .equalsIgnoreCase "
+ s2
+
": "
+ s1.equalsIgnoreCase(s2));
// Compare s1 and s3
// It should return true as they both
// have the same content being case insensitive
System.out.println(s1 +
" .equalsIgnoreCase "
+ s3
+
": "
+ s1.equalsIgnoreCase(s3));
// Compare s2 and s3
// It should return true as they both
// have the same content being case insensitive
System.out.println(s2 +
" .equalsIgnoreCase "
+ s3
+
": "
+ s2.equalsIgnoreCase(s3));
// Compare s1 and s4
// It should return true as they both
// have the same content
System.out.println(s1 +
" .equalsIgnoreCase "
+ s4
+
": "
+ s1.equalsIgnoreCase(s4));
Method 5: Using compare() method
In Java for locale specific comparison, one should use Collator class which is in java.text package. The one most important feature of Collator class is the ability to define our own custom comparison rules.
Below example illustrate the use of compare() method in Java to compare Strings:
// It should return 0 as they both
// have the same ASCII value
System.out.println(s1 +
" collator.compare "
+ s2
+
": "
+ collator.compare(s1, s2));
// Compare s1 and s3
// It should return 1
System.out.println(s1 +
" collator.compare "
+ s3
+
": "
+ collator.compare(s1, s3));
// Compare s3 and s2
// It should return -1
System.out.println(s3 +
" collator.compare "
+ s2
+
": "
+ collator.compare(s3, s2));
// Compare s1 and s4
// It should return 0 as they both
// have the same ASCII value
System.out.println(s1 +
" collator.compare "
+ s4
+
": "
+ collator.compare(s1, s4));