明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2324|回复: 8

怎样提取字符串中的数字

[复制链接]
发表于 2004-7-6 17:48:00 | 显示全部楼层 |阅读模式
如HW100X100X100X100中的数字提取也来分别赋值给A,B,C ,D。
发表于 2004-7-6 20:15:00 | 显示全部楼层
(defun C:test()
(mapcar 'set '(A B C D) (mapcar 'read (String_To_Numbers "100X100X100X100")))
)
; 从字符串中提取数据,返回表
; The following two subrs are written by Bill Kramer
; Copied from the book "AutoCADet's Guide to Visual Lisp" - 2002
;------------------------------------------------------ Listing 4.4
;
(defun String_To_Numbers (inStr
;;Input string
/ Res
;;Result list
Buf
;;String buffer
Inx
;;Character location
CH
;;Character
)
(setq Inx 1 ;start at the beginning of the string
Buf "" ;init buffer to empty
)
;
; Loop until the end of the string.
; (I indicates where we are in the string)
;
(while (<= Inx (strlen inStr))
;
; Get the character at position Inx, increment position indicator
(setq CH (substr inStr Inx 1)
Inx (1+ Inx)
)
;
(cond
; Test to see if character is a digit.
((wcmatch CH "[0-9.]")
(if (= CH ".") ;is it decimal?
(if (not (wcmatch Buf "*`.*")) ;not already in there
(setq Buf (strcat Buf CH))
(Flush_Buf)
)
;
(setq Buf (strcat Buf CH))
)
)
((= Buf "") ;is the buffer empty?
;Is CH minus
(if (= CH "-")
(setq Buf CH) ;Yes, save in Buf
)
)
('T ;else buffer is not empty
(Flush_Buf)
(if (= CH "-")
(setq Buf CH)
)
)
) ; End of COND
) ; End of WHILE
;
(if (and (/= Buf "")
(not (wcmatch Buf "[+-.]"))
)
(Flush_Buf)
)
(reverse Res)
)
;
; ------------------------------------------------------ Listing 4.5
;
(defun Flush_Buf ()
(if (not (wcmatch Buf "[+-.]")) ;is it not just +-.?
(progn
;Clean it up first
(if (= (substr Buf 1 1) ".")
(setq Buf (strcat "0" Buf))
)
;;add zero to front if .#
(if (= (substr Buf (strlen Buf)) ".")
(setq Buf (substr Buf 1 (1- (strlen Buf))))
)
;;remove decimal if #.
;Add to RES list
(setq RES (cons Buf RES))
)
)
(setq Buf "")
;;reset Buf
)
 楼主| 发表于 2004-7-6 21:56:00 | 显示全部楼层
非常感谢alin,你真是我的好老师,我又学了不少。
发表于 2004-7-7 09:12:00 | 显示全部楼层
这个针对性强些,通用性差点:),自己看着办
  1. (defun C:test()
  2.    (mapcar 'set '(A B C D) (read (strcat "(" (vl-string-translate     "XXXX" "       " "100X200X300X400") ")")))
  3.    (list A B C D)
  4. )
  5. ->(100 200 300 400)
 楼主| 发表于 2004-7-7 23:49:00 | 显示全部楼层
就象你说的一样,针对性强些,通用性差点,不过你的方法还好.我还有个问题想问一下.我怎样在CAD中将计算得到结果直接赋值给屏幕上某个字串,而不需要我手工改.
发表于 2004-7-8 08:46:00 | 显示全部楼层
ayan634发表于2004-7-7 23:49:00就象你说的一样,针对性强些,通用性差点,不过你的方法还好.我还有个问题想问一下.我怎样在CAD中将计算得到结果直接赋值给屏幕上某个字串,而不需要我手工改.
有点像老师在问学生
发表于 2004-7-8 10:50:00 | 显示全部楼层
ayan634发表于2004-7-7 23:49:00就象你说的一样,针对性强些,通用性差点,不过你的方法还好.我还有个问题想问一下.我怎样在CAD中将计算得到结果直接赋值给屏幕上某个字串,而不需要我手工改.

lsp方法:更新dxf1。 vla: vla-put-textstring
发表于 2004-7-8 10:59:00 | 显示全部楼层
  1. 以下是引用无痕在2004-7-8 10:50:04的发言:
复制代码
  1. lsp方法:更新dxf1。 vla:   vla-put-textstring
复制代码
楼主要的应该是所得的数值与屏幕上某个字串为list的结果
 楼主| 发表于 2004-7-8 21:22:00 | 显示全部楼层
谢谢各位,我知道了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-10-1 00:28 , Processed in 0.202361 second(s), 26 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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