【源码】指定字符分割字符串
本帖最后由 13816600495 于 2024-5-25 10:11 编辑(defun cl-string-split (str string / m n f n1 n2 list1 list2 list3 list4 lst)
(setq list1 (vl-string->list str) n1 (length list1) list2 (vl-string->list string) n2 (length list2) n 0 tt t)
(while (nth n list2)
(setq lst nil m (1- n))
(repeat n1
(setq lst (cons (nth (setq m (1+ m)) list2) lst))
)
(if (not (equal list1 (reverse lst)))
(progn
(setq list3 (cons (nth n list2) list3))
(setq n (1+ n))
)
(progn
(if (= n 0) (setq list4 (cons "" list4)))
(if (and f (= (- n f) n1)) (setq list4 (cons "" list4)))
(setq f n n (+ n n1))
(if list3 (setq list4 (cons (vl-list->string (reverse list3)) list4) list3 nil))
(if (not (nth n list2)) (setq list4 (cons "" list4)))
)
)
)
(if list3 (setq list4 (cons (vl-list->string (reverse list3)) list4)))
(reverse list4)
)
->用法:
输入:str--指定的字符串
输入:string--要分割的字符串
->用法举例:
(defun c:pp ()
(cl-string-split "--" "日期--2023--05--24")
)
->返回: ("日期" "2023" "05" "24")
(DEFUN STR2LST (STR DEL / POS)
(if (setq POS (VL-STRING-SEARCH DEL STR))
(PROGN (CONS (SUBSTR STR 1 POS) (STR2LST (SUBSTR STR (+ POS 1 (STRLEN DEL))) DEL)))
(PROGN (LIST STR))
)
)
(STR2LST "日期--2023--05--24" "--")
->返回: ("日期" "2023" "05" "24")
找到以前别人的
(defun ko->str-symlst (string strkey / po strlst xlen)
(setq xlen (1+ (strlen strkey)))
(while (setq po (vl-string-search strkey string))
(setq strlst (cons (substr string 1 po) strlst) string (substr string (+ po xlen))))
(reverse (cons string strlst)))
(ko->str-symlst "日期--2023--05--24" "--")
->返回: ("日期" "2023" "05" "24") ;;;分割字串
(defun gxl-StrParse (str del / pos lst)
(while (setq pos (vl-string-search del str))
(setq lst (cons (substr str 1 pos) lst)
str (substr str (+ pos 1 (strlen del)))
)
)
(if (= " " Del)
(vl-remove "" (reverse (cons str lst)))
(reverse (cons str lst))
)
) 写的太啰嗦了 字符串之所以叫字符串,其实它是字符组合成的数组,结果lisp要反向切割... 增强型字串分割处理
http://bbs.mjtd.com/forum.php?mod=viewthread&tid=109842&fromuid=202795
(出处: 明经CAD社区)
(defun strsplit(str splits / i a b)
(while(<""str)
(if(vl-remove'nil(mapcar'(lambda(x)(vl-string-search x str))splits))
(setq i(car(vl-sort(vl-remove'nil(mapcar'(lambda(x)(if(setq l(vl-string-search x str))(cons l x)))splits))
'(lambda(s1 s2)(<(car s1)(car s2)))))
a(cons(substr str 1(car i))a)b(cons(cdr i)b)
str(substr str(+(car i)(strlen(cdr i))1)))
(setq a(cons str a)b(cons "" b)str"")))
(list(reverse a)(reverse b)))
(STRSPLIT "12+34分隔35-454,22,33-45" '("+""-""*"","";"" ""=""分隔"))
==>(("12" "34" "35" "454" "22" "33" "45") ("+" "分隔" "-" "," "," "-" "")) :lol 终于把大神都钓出来了 czb203 发表于 2024-5-25 21:33
递归用的妙啊,这个代码太简洁了!学习了!
页:
[1]