Hello,
is there any other method to compare string in java beside using equal() and == operator ?
Thank you
A concise, practical follow‑up to the thread (thanks to , and ): beyond the basic reference/value debate, here are other, less obvious ways to compare strings and the real‑world gotchas to watch for.
Keep comparisons null‑safe and canonical
Objects.equals(a, b) (Java 7+) or the literal‑first pattern ("lit".equals(var)) to avoid NPEs. s.strip()/trim() and java.text.Normalizer to turn composed/decomposed forms into the same canonical form.import java.util.Objects;
import java.text.Normalizer;
// null-safe
if (Objects.equals(a, b)) { /* equal */ }
// unicode-normalize
String n1 = Normalizer.normalize(s1, Normalizer.Form.NFC);
String n2 = Normalizer.normalize(s2, Normalizer.Form.NFC);
if (n1.equals(n2)) { /* equal */ } Locale, accents and special comparisons
java.text.Collator (you can ignore accents/case with setStrength). toLowerCase(Locale.ROOT) / toUpperCase(Locale.ROOT) (avoids Turkish ‘i’ problems). regionMatches for case‑insensitive substring checks, contentEquals to compare a String with a CharSequence, and matches, startsWith, endsWith for pattern/prefix/suffix checks. For numeric text, parse (e.g., Integer.parseInt, BigInteger) and compare numbers instead of the raw strings.Collator coll = Collator.getInstance(Locale.FRANCE);
coll.setStrength(Collator.PRIMARY); // ignore accents & case
if (coll.compare(s1, s2) == 0) { /* equal for user locale */ }
if (s1.regionMatches(true, 0, s2, 0, 3)) { /* first 3 chars equal, ignore case */ } Quick decision guide
Objects.equals or literal‑first equals. Normalizer + Collator. regionMatches / startsWith. These cover the common pitfalls that cause surprising results even after the basics mentioned earlier.
Jump to Post— cwarn23 387It is also possible to use strvariable.equalsIgnoreCase(input) and it will compare the two strings while ignoring the difference between upper case and lower case letters. So in this method A==a and B==b etc. But besides that there isn't any other method that I am aware of.
Jump to Post— stultuske 1,129what are you trying to compare? the reference or the value?
why would you need another way?anyway, the 'compareTo' methods 'll work just as well.
cwarn: be careful how you explain what you say:
A==a => in light of Java this is false.
It is also possible to use strvariable.equalsIgnoreCase(input) and it will compare the two strings while ignoring the difference between upper case and lower case letters. So in this method A==a and B==b etc. But besides that there isn't any other method that I am aware of.
what are you trying to compare? the reference or the value?
why would you need another way?
anyway, the 'compareTo' methods 'll work just as well.
cwarn: be careful how you explain what you say:
A==a => in light of Java this is false.
thank you for your explanation
I am learning java, and I am confuse in some exercises that need a comparison
is compareTo will give a true for this : 5 == 05 ?
thank you
5 and 05 are ints, not objects, so yo ucan't use compareTo directly for those values, but:
5 == 05 is true (same numeric value)
"5" == "05" is false (different tsrings)
JamesCherrill: actually, an int would change the "05" to 5.
Cakka: 5 == 05
compareTo won't give any result for that, since you are not using the method. it also depends on whether we are talking about the String class or a class you wrote yourself with it's own implementation of compareTo.
anyway, compareTo will never return true. it will return an int.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.