gpt4 book ai didi

string - clojure 删除字符串中模式的最后一个入口

转载 作者:行者123 更新时间:2023-12-01 07:11:07 26 4
gpt4 key购买 nike

我在字符串的末尾有一个字符串和一些模式。我怎样才能在单词的末尾完全删除这个模式,但即使它存在于开头或中间,也仅此而已。例如,字符串是

PatternThenSomethingIsGoingOnHereAndLastPattern

我需要在最后删除“模式”,以便结果是
PatternThenSomethingIsGoingOnHereAndLast

我怎样才能做到这一点?

最佳答案

您的问题没有指定模式必须是正则表达式还是普通字符串。在后一种情况下,您可以使用简单的方法:

(defn remove-from-end [s end]
(if (.endsWith s end)
(.substring s 0 (- (count s)
(count end)))
s))

(remove-from-end "foo" "bar") => "foo"
(remove-from-end "foobarfoobar" "bar") => "foobarfoo"

有关正则表达式的变体,请参阅 answer of Dominic Kexel .

关于string - clojure 删除字符串中模式的最后一个入口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13705677/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com