明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 779|回复: 1

[源码] Lee Mac的分割函数Str2Lst扩展,自产自销

[复制链接]
发表于 2020-7-11 23:05 | 显示全部楼层 |阅读模式
;Str2Lst是Lee Mac的分割函数扩展论坛很多就不列出来了
;下面根据Lee Mac的分割函数扩展,支持多个分割字符
;调用(Str2Lst1 str Str1)或(Str2Lst2 str Str1)
;设通道字符为"|",可以根据个人需求修改str1中不同的字符用"|"分开
;不适用于叠字符和单字符同时存在(比如A和AA同时存在会有问题)
;方案1全部替换成通道符号,按通道符号分解
(defun Str2Lst1(str Str1  / n  sl  len lst)
  (setq s1 (Str2Lst Str1 "|"));设通道字符为"|",可以根据个人需求修改
  (setq len (length s1))
  (while (> len 0)
        (setq len(1- len))
        (while   (vl-string-search (nth len s1 )  str)
             (setq  str  (vl-string-subst  "|"   (nth len s1 )    str))        
        )
  )
  (setq lst (Str2Lst Str "|"))  
  (vl-remove-if  (function  (lambda (n) (= n "")  ))  lst)
)
;方案2全部,查找每一个字符的文字,排序后分解字符串,思路很啰嗦
(defun Str2Lst2 (str Str1  / n  sl sx len pos pos0 posx  strx poslist lst)
  (setq sl (Str2Lst Str1 "|"))
  (setq poslist '())
  (foreach sx sl
    (progn
      (setq len (strlen sx)) ;查找字符长度
      (setq strx str)
      (setq posx 0)
      (while (setq pos (vl-string-search sx strx)) ;查找字符串位置
         (setq poslist (cons (vl-list* (+ pos 1 posx) len) poslist)) ;位置和字符长度构成点对,添加到表
       (setq posx (+ pos posx len))
       (setq  strx    (substr strx (+ pos len 1)));新字符串
      )
    )
  )
  (setq poslist
        (vl-sort poslist (function  (lambda (e1 e2) (< (car E1) (car E2)))))) ;位置排序
  (setq  n 0)
  (setq  pos0 1)
  (while (setq  posn (nth n poslist ));取得第n个标识符的位置(2 . 2)
         (setq lst (cons (substr str pos0 (- (car posn) pos0)) lst))
   ;获取1到第一位置的的字符串传到表中
   (setq   pos0 (+ (car posn) (cdr posn))
                 n (1+ n)
         )
  )
  (vl-remove-if  (function  (lambda (n) (= n "")  ))  lst)
  (if (/= (setq  str (substr str pos0)) "")
      (reverse (cons str lst))
      (reverse lst)
  )
)


"觉得好,就打赏"
还没有人打赏,支持一下
发表于 2020-7-13 08:48 | 显示全部楼层

谢谢
来点实例好学习应用
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-5-8 21:24 , Processed in 0.552131 second(s), 26 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表