目录

replace-first

replace-first

replace函数用于使用新的字符串值替换字符串中的子字符串,但仅用于第一次出现的子字符串。 通过使用模式来搜索子字符串。

语法 (Syntax)

以下是语法。

(replace-first str pat replacestr)

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

Return Value - 通过正则表达式模式替换子字符串的新字符串,但仅限于第一次出现。

例子 (Example)

以下是Clojure中替换优先的示例。

(ns clojure.examples.example
   (:gen-class))
;; This program displays Hello World
(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)

上面的例子显示了replace和replace-first函数之间的区别。

输出 (Output)

上述程序产生以下输出。

abc789de789
abc789de123
↑回到顶部↑
WIKI教程 @2018