明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1569|回复: 7

[提问] 如何将用逗号隔开的文字分开

[复制链接]
发表于 2014-3-11 18:01:02 来自手机 | 显示全部楼层 |阅读模式
文本中有好多行用逗号隔开的文字,比如"图元1,图元2,图元3",在用read-line函数读出该行后,怎么将图元1,图元2,图元3提取出来呢?请高手们指教!
"觉得好,就打赏"
还没有人打赏,支持一下
发表于 2014-3-11 20:02:02 | 显示全部楼层
vl-string-left-trim或者vl-string-right-trim
不知道对不对
发表于 2014-3-11 20:11:08 | 显示全部楼层
  1. (defun c:fd ()
  2. (princ "打开文件所在文件夹\n选择文件:")
  3. (setq name(getfiled "指定文件" (getvar "dwgprefix") "dwg" 0))
  4. (setq position
  5. (vl-string-search "\\" name 4))
  6. (setq np position)
  7. (while (numberp position )
  8. (setq position
  9. (vl-string-search "\\" name (1+ position)))
  10. (if (/= position nil)(setq np position))
  11. )
  12. (setq tn(substr name 1 np))
  13. (mapcar 'princ (list "\n文件路径=" tn))
  14. (startapp "explorer" tn)
  15. (prin1)
  16. )


你根据这个改一改,就懒得给你具体弄了
发表于 2014-3-11 22:07:42 | 显示全部楼层
(setq zfc (read-line wjm))
(while (vl-string-search "," zfc)
     (setq zfc (vl-string-subst " " "," zfc))
)
(setq jgz (read (strcat "(" zfc ")")))
;jgz=(图元1  图元2 ...)
 楼主| 发表于 2014-3-14 12:59:47 | 显示全部楼层
yshf 发表于 2014-3-11 22:07
(setq zfc (read-line wjm))
(while (vl-string-search "," zfc)
     (setq zfc (vl-string-subst " " " ...

这个不行,我用的就是这个方法,但是得出的结果是图元1,图元2,图元3……而不是"图元1","图元2","图元3"……,就是说结果不是字符串,然后呢就没法和字符串进行比较,比如说是否等于?你可以试验下。
发表于 2014-3-14 19:55:52 | 显示全部楼层
本帖最后由 yshf 于 2014-3-14 20:00 编辑

要变字符串,再加一行不就了!
(setq jgz (mapcar '(lambda(x) (vl-princ-to-string x)) jgz))
或者
(setq jgz (mapcar 'vl-princ-to-string jgz))
发表于 2014-3-15 12:24:33 | 显示全部楼层
  1. ;; (Str2Lst "图元1,图元2,图元3" ",") → ("图元1" "图元2" "图元3")
  2. (defun Str2Lst (str sprstr / lst m n a)
  3.   (setq        lst '()
  4.         m   (strlen sprstr)
  5.   )
  6.   (while (setq n (vl-string-search sprstr str))
  7.     (setq a   (substr str 1 n)
  8.           str (substr str (+ n m 1))
  9.           lst (if (/= a "")
  10.                 (cons a lst)
  11.                 lst
  12.               )
  13.     )
  14.   )
  15.   (reverse (cons str lst))
  16. )
发表于 2014-3-21 11:47:10 | 显示全部楼层
本帖最后由 77077 于 2014-3-21 11:53 编辑

院长这次发的是真源码!赶紧收藏~~~
;;;
;;;读取文件并按行将文件转换为表 ;;;函数代码:
;;;(Txt2Lst "d:\\临时文件.txt")
  1. (defun Txt2Lst(files / tmplst x fn)  
  2.   (setq files(findfile files))
  3.   (if files
  4.     (progn
  5.       (setq fn (open  files "r"))
  6.       (while (setq x (read-line fn))
  7.         (setq txtlst(append txtlst (list x)))
  8.       )
  9.       (close fn)
  10.       txtlst
  11.     )
  12.      nil
  13.   )
  14. )
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-5-25 06:27 , Processed in 0.152791 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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