How To Compare 2 String Inwards Coffee - String Comparing Example
String comparing is a mutual programming business in addition to Java provides several agency to compare ii String inward Java. String is a special course of teaching inward Java, String is immutable in addition to It’s used a lot inward every unmarried Java plan starting from unproblematic seek to company Java application. In this Java String compare tutorial nosotros volition run across different ways to compare ii String inward Java in addition to uncovering out how they compare String in addition to when to role equals() or compareTo() for comparing etc.
Here are iv examples of comparing String in Java
1) String comparing using equals method
2) String comparing using equalsIgnoreCase method
2) String comparing using compareTo method
4) String comparing using compareToIgnoreCase method
We volition run across String compare Example using each of this method inward illustration section, before that let's larn or in addition to thus theory:
This article is inward continuation of my before post service on String e.g. Difference betwixt String in addition to StringBuffer inward Java in addition to How to supercede String inward coffee using regular expression etc. If you lot haven’t read them already you lot may uncovering them useful in addition to worth reading.
Compare ii String using equals method inward Java
Compare String using equalsIgnoreCase method inward Java
equalsIgnoreCase is to a greater extent than liberal than equals in addition to compare ii strings ignoring at that topographic point case. So if ii String contains same characters in addition to inward same guild despite of at that topographic point illustration e.g. lower case, upper case, Capital illustration or Camel illustration they volition live on equal past times equalsIgnoreCase.For illustration "sony" in addition to "SONY" ii Strings volition live on same past times equalsIgnoreCase in addition to it volition render truthful but "Sony" in addition to "Samsung" volition non live on same in addition to it volition render faux because they don't comprise same characters.
Comparing String using compareTo
compareTo are actual comparing method different equals()and equalsIgnoreCase() in addition to tell us whether ii Strings are lexicographically equal, precedes or follows each other. if you lot desire to variety Strings lexicographically compareTo() method is used. this is every bit good called natural guild of String. returns zero if ii Strings are same, less than zero if calling string comes before declaration string in addition to greater than zero if calling string comes afterward than declaration string every bit shown inward illustration below. See things to retrieve spell overriding compareTo method inward Java for more especial on compareTo method.
Compare String using compareToIgnoreCase
Similar to compareTo() method alongside ignoring illustration similar equalsIgnoreCase() in addition to render same values every bit returned past times compareTo during String comparison.
Don't role "==" for String comparison
Many Java programmer makes mistake of using "==" for string comparison. "==" simply banking concern fit if ii reference variable are pointing ii same object inward Java heap in addition to since String is immutable inward Java in addition to maintained inward String puddle ii String literal refer same String object which gives feel that "==" tin live on used to compare string which is incorrect. ever role equals() method for equality banking concern fit in addition to compareTo method for actual string comparison.
Another agency of comparing String is writing custom Comparator inward Java. write your comparing logic inward compare() method in addition to than you lot tin role that logic to compare ii strings.
public class StringComparisonExample {
public static void main(String args[]) {
String tv = "Bravia";
String goggle box = "Bravia";
// String compare illustration using equals
if (tv.equals(television)) {
System.out.println("Both tv in addition to goggle box contains same letters in addition to equal past times equals method of String");
}
// String compare illustration inward coffee using compareTo
if (tv.compareTo(television) == 0) {
System.out.println("Both tv in addition to goggle box are equal using compareTo method of String");
}
goggle box = "BRAVIA";
// Java String comparing illustration using equalsIgnoreCase
if (tv.equalsIgnoreCase(television)) {
System.out.println("tv in addition to goggle box are equal past times equalsIgnoreCase method of String");
}
// String comparing illustration inward coffee using CompareToIgnoreCase
if (tv.compareToIgnoreCase(television) == 0) {
System.out.println("tv in addition to goggle box are same past times compareToIgnoreCase of String");
}
String sony = "Sony";
String samsung = "Samsung";
// lexicographical comparing of String inward Java alongside ComapreTo
if (sony.compareTo(samsung) > 0) {
System.out.println("Sony comes after Samsung inward lexicographical order");
} else if (sony.compareTo(samsung) < 0) {
System.out.println("Sony comes before Samsung inward lexicographical order");
}
}
}
Output:
Both tv in addition to goggle box contains same letters in addition to equal past times equals method of String
Both tv in addition to goggle box are equal using compareTo method of String
tv in addition to goggle box are equal past times equalsIgnoreCase method of String
tv in addition to goggle box are same past times compareToIgnoreCase of String
Sony comes after Samsung inward lexicographical order
public static void main(String args[]) {
String tv = "Bravia";
String goggle box = "Bravia";
// String compare illustration using equals
if (tv.equals(television)) {
System.out.println("Both tv in addition to goggle box contains same letters in addition to equal past times equals method of String");
}
// String compare illustration inward coffee using compareTo
if (tv.compareTo(television) == 0) {
System.out.println("Both tv in addition to goggle box are equal using compareTo method of String");
}
goggle box = "BRAVIA";
// Java String comparing illustration using equalsIgnoreCase
if (tv.equalsIgnoreCase(television)) {
System.out.println("tv in addition to goggle box are equal past times equalsIgnoreCase method of String");
}
// String comparing illustration inward coffee using CompareToIgnoreCase
if (tv.compareToIgnoreCase(television) == 0) {
System.out.println("tv in addition to goggle box are same past times compareToIgnoreCase of String");
}
String sony = "Sony";
String samsung = "Samsung";
// lexicographical comparing of String inward Java alongside ComapreTo
if (sony.compareTo(samsung) > 0) {
System.out.println("Sony comes after Samsung inward lexicographical order");
} else if (sony.compareTo(samsung) < 0) {
System.out.println("Sony comes before Samsung inward lexicographical order");
}
}
}
Output:
Both tv in addition to goggle box contains same letters in addition to equal past times equals method of String
Both tv in addition to goggle box are equal using compareTo method of String
tv in addition to goggle box are equal past times equalsIgnoreCase method of String
tv in addition to goggle box are same past times compareToIgnoreCase of String
Sony comes after Samsung inward lexicographical order
These were or in addition to thus common examples of comparing string inward Java. Avoid mutual error of using equality operator “==” for comparing String instead ever role equals() method.
Further Learning
Data Structures in addition to Algorithms: Deep Dive Using Java
Why graphic symbol array is amend than String for storing password
Komentar
Posting Komentar