怎样优雅的处理字符串

wiwi

来自: wiwi 2014-05-21 09:44:49

×
加入小组后即可参加投票
  • 疯狂的噬菌体

    疯狂的噬菌体 ((loop(print(eval(read))))) 2014-05-25 17:25:04

    正则表达式,应该是你想要的。

  • isaac

    isaac 2014-10-01 01:24:38

    (-> your-str
    (str/replace #"([A-Za-z])\1+" "$1")
    (str/replace #"[^A-Za-z]+" "*"))

  • [已注销]

    [已注销] 2016-05-22 23:09:40

    发现 partition-by 这个函数在这里很好用:) 如果不用正则匹配的话,我试着写了一下:

    (require '[clojure.string])
    (require '[clojure.test :as t])

    (defn pred [x] (Character/isLetter x))

    (def replacement \*)

    (defn transform
    ..{:test
    ...#(do (t/is (= "" (transform "")))
    ........(t/is (= "a" (transform "a")))
    ........(t/is (= "ab" (transform "ab")))
    ........(t/is (= "*" (transform "*")))
    ........(t/is (= "*" (transform "1")))
    ........(t/is (= "*" (transform "**")))
    ........(t/is (= "*a*" (transform "((((a))))")))
    ........(t/is (= "A*z" (transform "A@!1z")))
    ........(t/is (= "hello*world*" (transform "hello123world!@#"))))}
    ..[s]
    ..(clojure.string/join
    ...(reduce
    ....(fn [r cs]
    ......(if (pred (first cs))
    ........(into r cs)
    ........(conj r replacement)))
    ....[] (partition-by pred s))))

你的回应

回应请先 , 或 注册

812 人聚集在这个小组
↑回顶部