无涯教程-Clojure - replace-first函数

54 阅读1分钟

replace-first

replace函数用于将字符串中的子字符串替换为新的字符串值,但仅适用于第一次出现的子字符串。

replace-first - 语法

(replace-first str pat replacestr)

参数      - pat是正则表达式模式, " str"是需要根据模式在其中找到文本的字符串, " replacestr"是需要根据模式在原始字符串中替换的字符串。

返回值  -  新字符串,其中子字符串的替换是通过正则表达式模式完成的,但仅在第一次出现时才进行替换。

replace-first - 示例

(ns clojure.examples.example
   (:gen-class))

;; This program displays Hello Learnfk (defn Example [] (def pat (re-pattern "\d+")) (def newstr1 (clojure.string/replace "abc123de123" pat "789")) (def newstr2 (clojure.string/replace-first"abc123de123" pat "789")) (println newstr1) (println newstr2)) (Example)

上面的程序产生以下输出。

abc789de789
abc789de123

参考链接

www.learnfk.com/clojure/clo…