I was wondering if it is possible to compare strings as if they were numbers. For instance is there any way you could make it so that "Cat" > "Dog"
You can't use operators (e.g. "Cat" < "Dog"
) as you suggest. As @larsmans says that would require operator overloading which Java doesn't provide. However, you can still compare strings using "Cat".compareTo("Dog")
which returns 0 if the strings are equal, a number greater than 0 if "Cat"
is lexicographically less than "Dog"
, or a negative number otherwise.
See this page
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With