检查字符串是否以clojure中的给定字符串结尾

时间:2013-07-30 03:43:17

标签: string clojure

我创建了一个函数来检查我的第一个字符串是否以第二个字符串结尾。

在Java中我们有现成的方法来检查这个,但在Clojure中我找不到这样的方法所以我编写了自定义函数如下:

(defn endWithFun [arg1 arg2] 
    (= (subs arg1 (- (count arg1) (count arg2)) (count arg1)) arg2))

输出:

> (endWithFun "swapnil" "nil")
true
> (endWithFun "swapnil" "nilu")
false

这是按预期工作的。

我想知道,有类似的选择吗? 同样在我的情况下,我比较敏感。我也想忽略区分大小写。

2 个答案:

答案 0 :(得分:14)

您可以直接在Clojure中访问本机Java endsWith

(.endsWith "swapnil" "nil")

有关详细信息,请参阅http://clojure.org/java_interop

然后你可以自然地将其组合起来以获得不区分大小写:

(.endsWith (clojure.string/lower-case "sWapNIL") "nil")

答案 1 :(得分:4)

Clojure 1.8在clojure.string中引入了ends-with?函数,所以现在有一个本机函数:

> (ends-with? "swapnil" "nil")
true
> (ends-with? "swapnil" "nilu")
false

如果你想要不区分大小写,请再次申请lower-case

相关问题