newer 发表于 2025-5-13 10:15:00

XD::STR:REPLACE


;;; ------------------------------------------------------------------------
;;; 函数名称 / Function Name: XD::STR:replace
;;;
;;; 【功能说明 / Description】
;;;   将输入字符串中所有指定的子字符串替换为目标子字符串。
;;;   Replaces all occurrences of a specified substring with the target substring.
;;;
;;; 【参数说明 / Parameters】
;;;   string - 原始字符串 / The original string
;;;   old-substr - 需要被替换的子字符串 / The substring to be replaced
;;;   new-substr - 替换成的目标子字符串 / The substring to replace with
;;;
;;; 【返回值 / Return Value】
;;;   返回一个新字符串,替换完成后的结果 / The new string after replacement
;;;
;;; 【依赖函数 / Dependencies】
;;;   无 / None
;;;
;;; 【使用示例 / Example Usage】
;;;   (XD::STR:replace "C:/A/B" "/" "\\")
;;;   => "C:\\A\\B"
;;;
;;; 【备注 / Notes】
;;;   - 此函数替换所有匹配的子字符串 / This function replaces all occurrences of the substring.
;;;   - 在使用时,请确保输入的字符串参数和目标子字符串符合预期。
;;; ------------------------------------------------------------------------

(defun XD::STR:replace (old-substr new-substr string / len-old pos result)
;; 【功能】替换字符串中所有匹配的子串
;; 【参数】string - 原始字符串, old-substr - 被替换子串, new-substr - 替换子串
;; 【返回】替换后的新字符串

(setq len-old (strlen old-substr))
(setq result "")

(while (setq pos (vl-string-search old-substr string))
    (setq result (strcat result (substr string 1 pos) new-substr))
    (setq string (substr string (+ pos len-old 1)))
)

(strcat result string)
)

页: [1]
查看完整版本: XD::STR:REPLACE