本帖最后由 kucha007 于 2023-5-1 16:52 编辑
分享两个关于字符串的函数:
- ;补齐字符串前导零,强制满足位数
- ;作者不知道是谁了,也是明经上看到的
- (defun K:AddZero4STR (STR Len)
- (repeat (- Len (strlen STR))
- (setq STR (strcat "0" STR))
- )
- STR
- )
- ;根据特殊符号分解文字为序号+内容两部分 by kucha
- ;Nam为序号识别的位数,如果在这个位数内找到特殊符号(" " "." "," "-" "_" "、")就拆分字符串。
- ;如果特殊符号出现多个或多次,则第一个符号之前的是序号,最后一个符号之后的是内容
- (defun K:StepSTRSplit (Nam STR / ExpSTR NumSTR DeLst StepNum NewLst)
- (progn
- ;将字符串拆分成单字符列表
- (defun K:ExplodeSTR (STR / Lst NewLst)
- (setq Lst (vl-string->list STR))
- (while
- (if (< (car Lst) 129) ;字符是否为单字节
- (setq NewLst (cons (chr (car Lst)) NewLst)
- Lst (cdr Lst)
- );单字节为数字、字母、符号(ASCII表)
- (setq NewLst (cons (strcat (chr (car Lst)) (chr (cadr Lst))) NewLst)
- Lst (cddr Lst)
- );双字节为汉字
- )
- )
- (reverse NewLst)
- )
- ;取出列表的前n项
- (defun K:TakeFirstN (Lst Nam / NewLst)
- (setq Nam (min Nam (length Lst)))
- (repeat Nam
- (setq NewLst (cons
- (nth (setq Nam (1- Nam)) Lst)
- NewLst
- )
- )
- )
- NewLst
- )
- ;判断字符串是否只包含数字字符
- (defun K:IsValidNumSTR (STR / Lst Flag)
- (setq Lst (vl-STRing->list STR))
- (setq Flag T)
- (while (and Flag Lst)
- (if
- (and
- (>= (car Lst) 48)
- (<= (car Lst) 57)
- )
- (setq Lst (cdr Lst))
- (setq Flag nil)
- )
- )
- Flag
- )
- )
- (setq ExpSTR (K:ExplodeSTR STR))
- (setq NumSTR (K:TakeFirstN ExpSTR Nam))
- (if
- (and
- (setq DeLst (vl-remove-if-not '(lambda (x) (member x '(" " "." "," "-" "_" "、"))) NumSTR))
- (setq StepNum (apply 'strcat (K:TakeFirstN NumSTR (- (length NumSTR)(length (member (car DeLst) NumSTR))))))
- (K:IsValidNumSTR StepNum)
- )
- (progn
- (repeat
- (- (min Nam (length NumSTR))
- (vl-position (last DeLst)(reverse NumSTR))
- )
- (setq ExpSTR (cdr ExpSTR))
- )
- (setq NewLst (list StepNum(apply 'strcat ExpSTR) ))
- )
- (setq NewLst (list "" STR))
- )
- NewLst
- )
|