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)

上面的程序产生以下输出。

链接:https://www.learnfk.comhttps://www.learnfk.com/clojure/clojure-regular-expressions-replacefirst.html

来源:LearnFk无涯教程网

abc789de789
abc789de123

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

技术教程推荐

透视HTTP协议 -〔罗剑锋(Chrono)〕

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

Service Mesh实战 -〔马若飞〕

分布式系统案例课 -〔杨波〕

用户体验设计实战课 -〔相辉〕

乔新亮的CTO成长复盘 -〔乔新亮〕

Spark性能调优实战 -〔吴磊〕

说透区块链 -〔自游〕

计算机基础实战课 -〔彭东〕

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