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

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

技术教程推荐

如何做好一场技术演讲 -〔极客时间〕

iOS开发高手课 -〔戴铭〕

高并发系统设计40问 -〔唐扬〕

Serverless入门课 -〔蒲松洋(秦粤)〕

高楼的性能工程实战课 -〔高楼〕

操作系统实战45讲 -〔彭东〕

说透区块链 -〔自游〕

Web 3.0入局攻略 -〔郭大治〕

超级访谈:对话玉伯 -〔玉伯〕

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