字符串转表[支持中文]
->任意字符串转表:;;输入:任意字符串
;;输出:列表
(defun cl-string->list (string / lst lst1 nn n m)
(setq nn (strlen string) n 0 m 1)
(repeat nn
(setq n (1+ n) lst (vl-string->list (substr string n m)))
(if (vl-remove-if '(lambda (x) (< x 128)) lst)
(progn
(if (= (length lst) 2)
(progn
(setq lst1 (cons (vl-list->string lst) lst1))
(if (= m 2) (setq n (1+ n) m 1))
)
(progn
(setq n (1- n) m 2)
)
)
)
(progn
(setq lst1 (cons (vl-list->string lst) lst1))
)
)
)
(reverse lst1)
)
->用法示例:
(defun c:pp ()
(cl-string->list "#$%文字@789")
)
;;返回:("#" "$" "%" "文" "字" "@" "7" "8" "9")
我觉得LLSheng_73这个函数写得挺好
;;字符串拆分为单一的表(支持单双字节混合)
(defun _Str2List(str / a);作者:LLSheng_73
(setq str(vl-string->list str))
(while
(if(<(car str)129)
(setq a(cons(chr(car str))a)str(cdr str))
(setq a(cons(strcat(chr(car str))(chr(cadr str)))a)str(cddr str))))
(reverse a)
)
;;字符串转表,中英文拆分为单独文字表-----(一级)------
;;(sl-str->singleonly "12我 的\n三领设计#$%文字@789")("1" "2" "我" " " "的" "\n" "三" "领" "设" "计")
(defun sl-str->singleonly (str / strlst strlst1 hz_str num)
(setqstrlst (vl-string->list str) strlst1 '())
(while strlst
(setq num (car strlst))
(cond
((and (not hz_str) (> num 159))
(setq hz_str (list num))
)
((and hz_str (> num 159))
(setq hz_str (append hz_str (list num)))
(setq strlst1 (append strlst1 (list hz_str)) hz_str nil)
)
((< num 159)
(setq hz_str nil)
(if strlst1
(setq strlst1 (append strlst1 (list (list num))))
(setq strlst1 (list (list num)))
)
)
)
(setq strlst (cdr strlst))
)
(mapcar 'vl-list->string strlst1)
)
;;字符串转表,中英文拆分为单独文字表-----(一级)------
;(cl-string->list "12我 的\n三领设计#$%文字@789")
(defun cl-string->list (string / lst lst1 nn n m)
(setq nn (strlen string) n 0 m 1)
(repeat nn
(setq n (1+ n) lst (vl-string->list (substr string n m)))
(if (vl-remove-if '(lambda (x) (< x 128)) lst)
(progn
(if (= (length lst) 2)
(progn
(setq lst1 (cons (vl-list->string lst) lst1))
(if (= m 2) (setq n (1+ n) m 1))
)
(setq n (1- n) m 2)
)
)
(setq lst1 (cons (vl-list->string lst) lst1))
)
)
(reverse lst1)
)
;;字符串转表,中英文拆分为单独文字表-----(一级)------
;(_Str2List "12我 的\n三领设计#$%文字@789")
(defun _Str2List (str / a);作者:LLSheng_73
(setq str (vl-string->list str))
(while
(if (< (car str) 129)
(setq a (cons (chr (car str)) a) str (cdr str))
(setq a (cons (strcat (chr (car str)) (chr (cadr str))) a) str (cddr str))
)
)
(reverse a)
)
正则表达式用"."就行 (defun c:tes ( / a1 opn ss1 tr1)
(setq a1 "中文2024");举例
(if (cadr (vl-string->list "中")) (setq opn nil) (setq opn t) );判断中文是1个还是2个字节
(setq ss1 '())
(while (/= a1 "")
(cond
(opn
(setq tr1 (substr a1 1 1) a1 (substr a1 2));中文与英文结合都是一个字节cad2024版本
)
(t
(if (> (ascii (substr a1 1 1)) 128)
(setq tr1 (substr a1 1 2) a1 (substr a1 3))
(setq tr1 (substr a1 1 1) a1 (substr a1 2))
)
)
)
(setq ss1 (cons tr1 ss1))
);while
(reverse ss1);字串符变表->("中" "文" "2" "0" "2" "4")
) 大哥字符串转表示啥意思 改 utf-8 编码,就不用再去判断双字节字符了
页:
[1]