Clojure 中的 replace

首页 / Clojure入门教程 / Clojure 中的 replace

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

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

零基础学Python -〔尹会生〕

Java性能调优实战 -〔刘超〕

编辑训练营 -〔总编室〕

NLP实战高手课 -〔王然〕

检索技术核心20讲 -〔陈东〕

互联网人的英语私教课 -〔陈亦峰〕

Selenium自动化测试实战 -〔郭宏志〕

WebAssembly入门课 -〔于航〕

郭东白的架构课 -〔郭东白〕

好记忆不如烂笔头。留下您的足迹吧 :)