最快的字串分割方法
本帖最后由 dcl1214 于 2024-2-19 17:58 编辑不知道这是不是有史以来最快的字串分割方法,盼望大家一起来挑战
;经过测试,高老师提供的方法更快,详见11楼
(defun $zi-chuan-fen-ge$ (str delim / lst i len)
(if (AND str delim (= (type str) 'str) (= (type delim) 'str))
(progn
(setq len (strlen delim))
(while (setq i (vl-string-search delim str))
(setq lst (cons (substr str 1 i) lst))
(setq str (substr str (+ 1 len i)))
)
(setq lst (cons str lst))
(reverse lst)
)
)
)
本帖最后由 yxp 于 2024-3-5 15:53 编辑
函数: (split str p)
用法:用 p 短字符串分割 str 长字符串,返回表,p 支持管道符多个短字符串并列
示例:(split “abcfarecadefge” “c|f”) 返回 (“ab” “are” “ade” “ge”)
(defun split (s p / r)
(setq r (vlax-create-object "vbscript.regexp"))
(vlax-put-property r 'Global 1)
(vlax-put-property r 'Pattern p)
(read (strcat "(\"" (vlax-invoke r 'Replace s "\" \"") "\")"))
)
来自:https://blog.csdn.net/yxp_xa/article/details/72636232
(defun $zi-chuan-fen-ge$ (str fgf / jg)
;字符串分割
(if (and str (= (type str) 'str) fgf (= (type fgf) 'str))
(progn
(setq str (vl-prin1-to-string str))
(while (wcmatch str (STRCAT "[,*`" FGF "*,]"))
(setq str (vl-string-subst "\"\"" fgf str))
)
(setq jg (read (strcat "(" str ")")))
)
)
jg
)
(defun syz-zi-chuan-fen-ge (str fgf)
;(setq fgf "明经")
(if (and str (= (type str) 'str) fgf (= (type fgf) 'str))
(progn
(setq i 1
k 1
)
(setq jg nil)
(while (< i (+ 1 (- (strlen str) (strlen fgf))))
(setq str2 (substr str i (strlen fgf)))
(if (equal str2 fgf)
(progn
(setq jg (append jg (list (substr str k (- i k)))))
(setq k (+ i (strlen fgf)))
(setq i (+ i (strlen fgf)))
)
(setq i (+ 1 i))
)
)
)
)
)
(setq str "s")
(setq j 0)
(repeat 6000
(setq str (strcat str
"测试字符串1234567阿布炒大白菜的abcd"
(itoa j)
"明经"
)
)
(setq j (+ 1 j))
)
(princ (strlen str))
(setq t0 (getvar "TDUSRTIMER"))
(syz-zi-chuan-fen-ge str "明经")
;($zi-chuan-fen-ge$ str "明经")
(princ "\n程序共用时")
(princ (* (- (getvar "TDUSRTIMER") t0) 86400))
(princ "秒")
(princ) 本帖最后由 highflybird 于 2024-2-19 16:15 编辑
baitang36 发表于 2024-2-14 08:58
(defun $zi-chuan-fen-ge$ (str fgf / jg)
;字符串分割
(if (and str (= (type str) 'str) fgf...
用read和vl-princ-to-string可能会有某些意想不到的效果。
我的代码:
(defun mysplit (str delim / lst i len)
(setq len (strlen delim))
(while (setq i (vl-string-search delim str))
(setq lst (cons (substr str 1 i) lst))
(setq str (substr str (+ 1 len i)))
)
(setq lst (cons str lst))
(reverse lst)
) 如果要用 "\"来做分隔符,估计会麻烦 顶一个,看高手来讨论 楼主这“有史以来最快的字串分割方法”一言难尽啊:lol,虽然清楚楼主在AutoLISP/Visual LISP 编程技术板块发的贴。 神仙约架了。。。 从哪抄的代码、怎么一会大写一会小写呢
神仙约架了。。。;P;P;P;P;P;P 学习了,谢谢分享 支持lispsys=1的情况吗
页:
[1]
2