Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check the equality of strings in clojure without invoking java.lang.String?

Tags:

clojure

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.

like image 505
Maksim Skurydzin Avatar asked Feb 25 '10 21:02

Maksim Skurydzin


People also ask

How do you know if two strings are equal in Clojure?

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 .

How do you check whether the String is equal or not?

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.

How do you know if two strings have the same value?

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 .

What is the best way to compare strings?

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.


1 Answers

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.

like image 107
Stuart Sierra Avatar answered Sep 19 '22 17:09

Stuart Sierra