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

swa*_*apy 8 string clojure

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

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

(defn endWithFun [arg1 arg2] 
    (= (subs arg1 (- (count arg1) (count arg2)) (count arg1)) arg2))
Run Code Online (Sandbox Code Playgroud)

输出:

> (endWithFun "swapnil" "nil")
true
> (endWithFun "swapnil" "nilu")
false
Run Code Online (Sandbox Code Playgroud)

这是按预期工作的.

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

Emi*_*Sit 14

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

(.endsWith "swapnil" "nil")
Run Code Online (Sandbox Code Playgroud)

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

然后,您可以自然地将其组合以获得不区分大小写:

(.endsWith (clojure.string/lower-case "sWapNIL") "nil")
Run Code Online (Sandbox Code Playgroud)


Bra*_*och 5

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

> (ends-with? "swapnil" "nil")
true
> (ends-with? "swapnil" "nilu")
false
Run Code Online (Sandbox Code Playgroud)

同样,lower-case如果您希望不区分大小写,只需应用。