Clojure Regular Expressions replace

2018-12-23 15:38 更新

replace

replace函數(shù)用于將字符串中的子字符串替換為新的字符串值。 使用模式來(lái)搜索子字符串。

下面是 replace 的使用示例:

(replace str pat replacestr)

參數(shù) ? 'pat'是正則表達(dá)式模式。 'str'是基于模式需要找到文本的字符串。 'replacestr'是需要在基于模式的原始字符串中替換的字符串。

返回值 ? 新字符串,其中子字符串的替換是通過(guò)正則表達(dá)式模式完成的。

下面是 replace 的使用示例:

(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)

輸出

以上示例將輸出以下結(jié)果:

abc789de
以上內(nèi)容是否對(duì)您有幫助:
在線(xiàn)筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)