Hello,

is there any other method to compare string in java beside using equal() and == operator ?

Thank you

Dani AI

Generated

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

  • Use Objects.equals(a, b) (Java 7+) or the literal‑first pattern ("lit".equals(var)) to avoid NPEs.
  • Normalize whitespace and Unicode before comparing: use 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

  • For user-facing, language‑sensitive comparison use java.text.Collator (you can ignore accents/case with setStrength).
  • For case mapping that’s predictable across locales use toLowerCase(Locale.ROOT) / toUpperCase(Locale.ROOT) (avoids Turkish ‘i’ problems).
  • Use 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

  • Nulls possible → Objects.equals or literal‑first equals.
  • User/display text → Normalizer + Collator.
  • Numeric strings → parse and compare numerically.
  • Substrings/prefixes → regionMatches / startsWith.

These cover the common pitfalls that cause surprising results even after the basics mentioned earlier.

Recommended Answers

All 5 Replies

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.