【K:ExplodeSTR】将字符串拆分成单字符列表
看到坛友在讨论拆分字符串,提供一个另类的拆分函数。
主要用在我另外两个帖子中的程序:DZ和FA
;将字符串拆分成单字符列表
(defun K:ExplodeSTR (STR / Defined STRDefined RtnSTR Pos)
(setq Defined
(List
"%%130" "%%131" "%%132" "%%133" "%%134" ;1~4级钢筋符号+特殊钢筋
"%%135" "%%136" "%%137" "%%138" ;L型钢+H型钢+槽型钢+工字钢
"%%150" "%%151" "%%152" "%%153" "%%154" "%%155" "%%156" "%%157" "%%158" "%%159" ;罗马数字
"%%177" "%%p" "%%P" ;正负号
"%%c" "%%C" "%%D" "%%d" "%%%";直径符号+度数符号+%
'("%%140" "%%141") '("%%142" "%%143") ;上下标开关
'("%%200" "%%201") '("%%202" "%%203") '("%%204" "%%205") ;圆圈数字
)
)
(while (> (strlen STR) 0)
(cond
((setq STRDefined
(vl-some
'(lambda (Sub / STRDefined)
(setq STRDefined (if (listp Sub) (car Sub) Sub))
(if (eq (substr STR 1 (strlen STRDefined)) STRDefined) Sub)
)
Defined
)
)
(if (listp STRDefined)
(if (setq Pos (vl-string-search (cadr STRDefined) STR))
(progn
(setq RtnSTR (cons (substr STR 1 (+ Pos (strlen (cadr STRDefined)))) RtnSTR))
(setq STR (substr STR (+ 1 Pos (strlen (cadr STRDefined)))))
)
(progn
(setq RtnSTR (cons STR RtnSTR))
(setq STR "");清空
)
)
(progn
(setq RtnSTR (cons STRDefined RtnSTR))
(setq STR (substr STR (1+ (strlen STRDefined))))
)
)
)
((> (ascii (substr STR 1 1)) 128) ;大于128为汉字
(if
(and
(>= (atoi (substr (ver) 13)) 2021)
(/= (getvar "lispsys") 0)
)
(progn
(setq RtnSTR (cons (substr STR 1 1) RtnSTR))
(setq STR (substr STR 2))
)
(progn
(setq RtnSTR (cons (substr STR 1 2) RtnSTR))
(setq STR (substr STR 3))
)
)
)
(T
(setq RtnSTR (cons (substr STR 1 1) RtnSTR))
(setq STR (substr STR 2))
)
)
)
(reverse RtnSTR)
)
[*];;将字符串拆解成单字列表-------(一级)--------(sl_text:stringexplode "12我 的\n三领设计")
[*];str_Given 需要转换的字符串
[*];b_Find 是否找到特殊字符串
[*];str_Defined 特殊字符串
[*];i_Length 特殊字符串 长度
[*];lst_Return 返回字符串列表
[*](defun sl_text:stringexplode (str_given / strlst_def strlst_def2 b_findstr_definedi_length lst_return e strlst-tmp)
[*](setq strlst_Def (str->lst "%%130|%%131|%%132|%%133|%%134|%%135|%%136|%%p|%%P|%%c|%%C" "|")) ;单定义字符
[*](setq strlst_Def2 (mapcar '(lambda (e) (str->lst e "*")) (str->lst "%%140*%%141|%%142*%%143|%%200*%%201|%%202*%%203|%%204*%%205" "|")));成对定义字符
[*];(("%%140" "%%141") ("%%142" "%%143") ("%%200" "%%201") ("%%202" "%%203") ("%%204" "%%205"))
[*](while (> (strlen str_given) 0);检查单字符串是否在特殊字符串列表中
[*] (setq b_find nil)
[*] ;检查 单定义字符
[*] (setq strlst-tmp strlst_def)
[*] (while (and strlst-tmp (not b_find))
[*] (setq str_defined (car strlst-tmp))
[*] (setq i_length (strlen str_defined))
[*] (if (= (substr str_given 1 i_length) str_defined)
[*] (setq b_find t)
[*] )
[*] (setq strlst-tmp (cdr strlst-tmp))
[*] )
[*] (setq strlst-tmp strlst_def2) ;检查 成对定义字符
[*] (while (and strlst-tmp (not b_find))
[*] (setq str_defined (caar strlst-tmp))
[*] (setq i_length (strlen str_defined))
[*] (if (= (substr str_given 1 i_length) str_defined)
[*] (setq b_find t str_defined (car strlst-tmp))
[*] )
[*] (setq strlst-tmp (cdr strlst-tmp))
[*] )
[*] (cond
[*] (b_find;特殊字符串
[*] (if (equal (type str_defined) 'str)
[*] (setq lst_return (cons str_defined lst_return)
[*] str_given (substr str_given (1+ i_length))
[*] )
[*] (if (setq i (vl-string-search (cadr str_defined) str_given))
[*] (setq e (substr str_given 1 (+ i (strlen (cadr str_defined))))
[*] lst_return (cons e lst_return)
[*] str_given (substr str_given (+ 1 i (strlen (cadr str_defined))))
[*] )
[*] (setq lst_return (cons (car str_defined) lst_return)
[*] str_given (substr str_given (1+ i_length))
[*] )
[*] )
[*] )
[*] )
[*] ((> (ascii (substr str_given 1 1)) 128);大于128为汉字
[*] (setq lst_return (cons (substr str_given 1 2) lst_return))
[*] (setq str_given (substr str_given 3))
[*] )
[*] (t
[*] (setq lst_return (cons (substr str_given 1 1) lst_return))
[*] (setq str_given (substr str_given 2))
[*] )
[*] )
[*])
[*](reverse lst_return)
[*])
感谢分享,学习下
页:
[1]