字符串分割-----------集中营
本帖最后由 自贡黄明儒 于 2013-8-14 14:39 编辑;;85.1 [功能] 字符串分割
;;http://www.mjtd.com/function/info-129-216.html,有修改 黄明儒 2013年8月9日
;;(setq str "100,,23456.876,345.456," delim ",")
(defun parse1 (str delim / LST POS)
(while (setq pos (vl-string-search delim str))
(setq lst (cons (substr str 1 pos) lst)
str (substr str (+ pos 2))
)
)
(if (> (strlen str) 0)
(setq lst (cons str lst))
)
(reverse lst)
)
;;85.2 [功能] 字符串分割
;;http://www.mjtd.com/function/info-129-444.html,修改xl:read->biao黄明儒 2013年8月9日
(defun parse2 (str delim / LST POS)
(while (setq pos (vl-string-search delim str))
(setq lst (append lst (list (substr str 1 pos))))
(setq str (substr str (+ 2 pos)))
)
(if (> (strlen str) 0)
(append lst (list str))
lst
)
)
**** Hidden Message *****
;;85.4 [功能] 字符串分割
;;这是一个很牛的分割法(基本同parse8),容易理解
;;delim是一个字符串集合,其中的每一个字符都会被当作是分割符号 by qjchen@gmail.com
;;如 (parse4 "符号25 35 45 ; 55, 66 " " ;")=> ("25" "35" "45" "55," "66")
;;(parse4 "aa ,10 b.10x20,.2" ",.")("aa " "10 b" "10x20" "2")
(defun parse4 (str delim / L1 L2)
(setq str (vl-string->list str)
delim (vl-string->list delim)
)
(while str
(if (not (member (car str) delim))
(setq l1 (cons (car str) l1))
(if l1
(setq l2 (cons (vl-list->string (reverse l1)) l2)
l1 nil
)
)
)
(setq str (cdr str))
)
(if l1
(setq l2 (cons (vl-list->string (reverse l1)) l2))
)
(reverse l2)
)
;;85.5 [功能] 字符串分割(这highflybird回答我的求助,简洁)
;;(parse5 "aa 10 b10x20.2" "")返回("AA" "10" "B10X20")
;;(parse5 "aa 10 b10x20.2" ".")返回("AA" "10" "B10X20" "2")
(defun parse5 (String Delimiter / str lst)
;;替换成空格,为后面转化作准备,问题是如果字符中原来就有空格
(setq str (VL-STRING-TRANSLATE Delimiter " " String))
(setq str (strcat "(" str ")")) ;加括号
(setq lst (read str)) ;读
(mapcar 'VL-PRINC-TO-STRING lst) ;转化
)
;;85.6 [功能] 用分隔符解释字符串成表 ;by fsxm
;;空格" ",不能用"" ,一个空格就转成一个字符
;;(parse6 "aa 10 b10x20.2" " ")返回("aa" "" "" "10" "b10x20.2")
;;(parse6 "aa 10 b10x20.2" "")死循环
(defun parse6 (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))
(setq string (substr string (+ po xlen)))
)
(reverse (cons string strlst))
)
;;85.7 [功能] 字符串分割 黄明儒 2013年8月9日
;;改自梁雄啸str2lst1;同parse5,也parse6一样,空格不能有""替代
;;(parse7 "aa 10 b10x20.2" " ");("aa" "" "" "10" "b10x20.2")
;;(parse7 "aa 10 b10x20.2" "")死循环
;;(parse7 "aa 10 b10x20.2" " ")("aa" "10" "b10x20.2")与parse5不同,不会去掉.2
;;(parse7 "aa 10 b10x20.2" "x")("aa 10 b10" "20.2")
(defun parse7 (str delim / I)
(while (setq i (vl-string-search
delim
str
(if i
(+ 2 i)
0
)
)
)
(setq str (vl-string-subst "\"\"" delim str i))
)
(read (strcat "(\"" str "\")"))
)
;;85.8 [功能] 字符串分割(基本同parse4)
;;改自梁雄啸str2lst2 黄明儒 2013年8月9日
;;(parse8 "aa ,10 b.10x20,.2" ",.")("aa " "10 b" "10x20" "" "2")
;;(vl-list->string '(40));"("
;;(vl-list->string '(34)),"\""
;;(vl-list->string '(32))," "
(defun parse8 (str delim / DELIM X)
(setq delim (vl-string->list delim))
(read
(vl-list->string
(apply
'append
(mapcar '(lambda (x)
(if (member x delim)
(list 34 32 34)
(list x)
)
)
(append (list 40 34) (vl-string->list str) (list 34 41))
)
)
)
)
)
;;85.9[功能] 字符串分割 (纯lisp法)
;;改自梁雄啸str2lst3 黄明儒 2013年8月9日
;;(parse9 "aa 10 b10x20.2" " ");("aa" "" "" "10" "b10x20.2")
;;(parse9 "aa 10 b10x20.2" ""),没作用,但不进入死循环
(defun parse9 (str delim / I S STR1 STRLST)
(setq i 0
str1 ""
)
(while (/= "" (setq s (substr str (setq i (1+ i)) 1)))
(cond ((/= delim s) (setq str1 (strcat str1 s)))
(T
(setq strlst (append strlst (list str1))
str1 ""
)
)
)
)
(if (/= str1 "")
(append strlst (list str1))
strlst
)
)
;;85.10 [功能] 字符串分割(纯lisp法)
;;改自梁雄啸str2lst 黄明儒 2013年8月9日
;;(parse10 "aa 10 b10x20.2" " ");("aa" "" "" "10" "b10x20.2")
;;(parse10 "aa 10 b10x20.2" ""),没作用,但不进入死循环
(defun parse10 (str delim / I S STR1)
(setq i 0
str1 ""
)
(while (/= "" (setq s (substr str (setq i (1+ i)) 1)))
(setq str1 (strcat str1
(if (= delim s)
"\" \""
s
)
)
)
)
(read (strcat "(\"" str1 "\")"))
)
;;85.12 [功能] 字符串分割 By st788796
;;基本同上parse4 parse8
;;(parse12 "aa ,10 b.10x20,.2" ",."),("aa ,10 b.10x20" "2")
;;(parse12 "aa ,10 b.10x20,.2" "")死循环
(defun parse12 (str delimiter / POST STRL STRLST)
(setq strl (strlen delimiter))
(while (vl-string-search delimiter str)
(setq post (vl-string-search delimiter str))
(setq strlst (append strlst (list (substr str 1 post))))
(setq str (substr str (+ post (1+ strl))))
)
(vl-remove "" (append strlst (list str)))
) 差不多!;按分隔符分解字符串成表.
(defun t11 (str del / pos lst)
(while
(setq pos (vl-string-search del str))
(setq lst (cons (substr str 1 pos) lst)
str (substr str (+ 1 pos (strlen del))
)
)
)
(reverse(cons str lst))
) (defun split2 (str sp / str1)
(cond
((= str nil) nil)
((= sp nil) nil)
((= str "") "")
((= sp "") str)
(t
(setq str1 (strcat "((\"" str "\"))"))
(while (vl-string-search sp str1)
(setq str1 (vl-string-subst "\")(\"" sp str1))
)
(apply 'append (read str1))
)
)
)
真心看不懂,太复杂了 哦,感谢黄兄,收集的这么全,谢谢了
支持 先占个沙发,感谢黄工! 看看,学习下 牛啊,收集的不少啊 感谢黄老师! 看看,参考学习 楼主辛苦了,希望能再整理些好东西