目录

replace

replace

replace函数用于用新的字符串值替换字符串中的子字符串。 通过使用模式来搜索子字符串。

语法 (Syntax)

以下是语法。

(replace 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 newstr (clojure.string/replace "abc123de" pat "789"))
   (println newstr))
(Example)

输出 (Output)

上述程序产生以下输出。

abc789de
↑回到顶部↑
WIKI教程 @2018