replace函数用于将字符串中的子字符串替换为新的字符串值,但仅适用于第一次出现的子字符串。
(replace-first str pat replacestr)
参数 - 'pat'是正则表达式模式, " str"是需要根据模式在其中找到文本的字符串, " replacestr"是需要根据模式在原始字符串中替换的字符串。
返回值 - 新字符串,其中子字符串的替换是通过正则表达式模式完成的,但仅在第一次出现时才进行替换。
(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
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)