Hello,

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

Thank you

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.

@cwarn23
thank you for your explanation

@stultuske
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.