马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
 - ;;; ------------------------------------------------------------------------
- ;;; 函数名称 / Function Name: XD::STR:m-find
- ;;;
- ;;; 【功能说明 / Description】
- ;;; 查找目标字符串 B 中所有等于子串 A 的起始位置索引。
- ;;; Finds all starting positions of substring A in string B.
- ;;;
- ;;; 【参数说明 / Parameters】
- ;;; a - 要查找的子字符串(区分大小写)。
- ;;; Substring to search for (case-sensitive).
- ;;; b - 要搜索的目标字符串。
- ;;; The target string where the search is performed.
- ;;;
- ;;; 【返回值 / Return Value】
- ;;; 返回一个整数列表,表示 A 在 B 中所有出现的位置(以 1 为起始)。
- ;;; Returns a list of integers indicating the 1-based positions of A in B.
- ;;;
- ;;; 【使用示例 / Example Usage】
- ;;; (XD::STR:m-find "AB" "ABCABC") => '(1 4)
- ;;; (XD::STR:m-find "," "A,B,C,") => '(2 4 6)
- ;;;
- ;;; 【备注 / Notes】
- ;;; - 若 A 为空字符串,返回 nil。
- ;;; Returns nil if A is an empty string.
- ;;; - 若 B 不是字符串类型,也返回 nil,并输出错误信息。
- ;;; Returns nil and prints an error if B is not a string.
- ;;; ------------------------------------------------------------------------
- (defun XD::STR:m-find (a b / c n)
- (cond
- ((= "" a)
- ;; A 为空,直接返回 nil / Empty substring
- (setq c nil)
- )
- ((/= 'str (type b))
- ;; B 不是字符串类型 / Type check failed
- (print "ARGUMENT NOT A STRING!!!!")
- (print b)
- (setq c nil)
- )
- (t
- ;; 开始查找匹配 / Start scanning
- (setq n 1)
- (while (and (>= (+ (- (strlen b) n) 1) (strlen a)))
- (if (= (substr b n (strlen a)) a)
- (progn
- ;; 找到匹配,记录位置 / Match found
- (setq c (append c (list n)))
- ;; 跳过当前匹配长度,防止重叠匹配 / Skip over match length
- (setq n (- (+ n (strlen a)) 1))
- )
- )
- (setq n (1+ n))
- )
- )
- )
- c
- )
|