Is there any way in clojure to check the equality of strings? i.e. I need to know, whether their contents is equal, not location.
thanks.
Equality in Clojure (the = function) always tests value, not identity, so two strings are = if they have the same contents. For most Java types, including String, Clojure = dispatches to Java . equals .
The equals() method compares two strings, and returns true if the strings are equal, and false if not. Tip: Use the compareTo() method to compare two strings lexicographically.
In String, the == operator is used to comparing the reference of the given strings, depending on if they are referring to the same objects. When you compare two strings using == operator, it will return true if the string variables are pointing toward the same java object. Otherwise, it will return false .
The right way of comparing String in Java is to either use equals(), equalsIgnoreCase(), or compareTo() method. You should use equals() method to check if two String contains exactly same characters in same order. It returns true if two String are equal or false if unequal.
Equality in Clojure (the =
function) always tests value, not identity, so two strings are =
if they have the same contents.
For most Java types, including String, Clojure =
dispatches to Java .equals
. String.equals is defined as "represents the same sequence of characters."
If you want to test identity (Are these pointers to the same location in memory?) use the identical?
function.
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