I have created a function to check whether or not my first string ends with a second string.
In Java we have ready made method to check this, but in Clojure I failed to find such a method so I written custom function as follows:
(defn endWithFun [arg1 arg2]
(= (subs arg1 (- (count arg1) (count arg2)) (count arg1)) arg2))
Outputs:
> (endWithFun "swapnil" "nil")
true
> (endWithFun "swapnil" "nilu")
false
This is working as expected.
I want to know, is there a similar alternative? Also in my case, I compare case sensitively. I also want to ignore case sensitivity.
You can access the native Java endsWith
directly in Clojure:
(.endsWith "swapnil" "nil")
See http://clojure.org/java_interop for some more details.
You can then naturally compose this to get case insensitivity:
(.endsWith (clojure.string/lower-case "sWapNIL") "nil")
Clojure 1.8 introduced an ends-with?
function in clojure.string, so now there is a native function:
> (ends-with? "swapnil" "nil")
true
> (ends-with? "swapnil" "nilu")
false
Again, just apply lower-case
if you want case insensitivity.
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