明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1968|回复: 6

关于文字前后空格

[复制链接]
发表于 2009-10-8 01:08:00 | 显示全部楼层 |阅读模式

在写一段程序,写完后发现文字中间如有空格程序执行还ok,如果前后有空格就不能算出来了,怎样让程序乎略掉前后的空格(文字中间的空格不能乎略)

下面是源程序和一个测试dwg档,求教大哥们开导一下,谢谢!(想搜索以前的相关贴子,搜索功能不好用,只能求教大家帮忙了)

;check_modifytxt
(defun c:tt(/ ckent1 ckent2 txl ppa ppt pt ft fv con pkt txx
  txv ppc ccn cn cv stt ct cm txtv conn tu1 tu2)
  (setq m:err *error* *error* *merr*)
  (command "undo" "be")
(defun *error*(msg)
(if (and (not (wcmatch msg "*函数被取消")) (/= msg "quit / exit abort"))
(princ (strcat "\n" msg))
)
(command "undo" "e")
(setvar "cmdecho" 0)
(setq *error* nil)
(princ)
)
  (prompt "\nSelect text to comp and modify:")
  (SETQ Ppa(SSGET '((0 . "TEXT"))))
  (if ppa
    (progn
      (SETQ Ppt(ssadd))
      (SETQ Ppc(ssadd) cm 0)
      (repeat (sslength ppa)
        (setq txtv (ssname ppa cm))
        (if (>(strlen(cdr (assoc 1 (entget txtv))))8)
           (setq ppc(ssadd txtv ppc))
           (setq ppt(ssadd txtv ppt))
        )
        (setq cm (1+ cm))
      )
      (while(> (sslength ppt) 0)
        (progn
          (setq ft(entget(ssname ppt 0)))
          (setq fv(assoc 1 ft))
          (command "select" ppt "")
          (setq pkt(ssget "p" (list fv)))
          (setq txl(sslength pkt) tu1 0 txx txl)
          (repeat (- txx 1)
            (setq ckent1 (ssname pkt tu1) tu2 0)
            (repeat (- txx tu2 tu1 1)
              (setq ckent2 (ssname pkt (+ tu2 tu1 1)))
              (if (< (distance (cdr (assoc 10(entget ckent1)))
                  (cdr (assoc 10(entget ckent2)))) 0.00001)
                  (setq txl(1- txl))
              )
              (setq tu2(1+ tu2))
            )                 
            (setq tu1(1+ tu1))
          )                                                    ;(repeat (- txx 1)
          (setq conn 0)
          (repeat (sslength ppc)
            (setq ct(entget(ssname ppc conn)))
            (setq cv(cdr(assoc 1 ct)) cn 1 ccn 1)
            (while (and(< cn (strlen cv))(/=(substr cv cn 1)":"))
              (setq cn (1+ cn))
            )
            (while (and(< ccn (strlen cv))(/=(substr cv ccn 1)"-"))
              (setq ccn (1+ ccn))
            )
            (if (= (substr cv cn 1) ":")
               (progn
                  (setq txv(strcat(cdr fv)":"(rtos txl 2 0)"-"(substr cv (1+ cn)(strlen cv))))
                  (if (= (substr cv ccn 1) "-")
                    (setq txv(strcat (cdr fv)":"(rtos txl 2 0)"-"(substr cv(1+ ccn)(strlen cv))))
                  )
               )
             )
             (if txv
                (if (and(= (substr txv 1 cn)(substr cv 1 cn))(< cn (strlen cv)))
                   (if (/=(substr txv 1 ccn)(substr cv 1 ccn))
                      (progn
                         (princ "\nText change:\t" )                        
                         (princ (strcat cv " --> " txv))
                           (setq stt (subst (cons 1 txv) (assoc 1 ct) ct))
                           (entmod stt)
                           (entupd (ssname ppc conn))
                      )
                    )
                 )
              )
              (setq txv nil)
              (setq conn (1+ conn))
            )                                                      ;(repeat (sslength ppc)
            (setq con 0)
            (repeat (sslength pkt)
               (setq ppt(ssdel (ssname pkt con)ppt) con (1+ con))
            )
         )
      )
    )
  )
  (*error* "")
)

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x
"觉得好,就打赏"
还没有人打赏,支持一下
发表于 2009-10-8 09:31:00 | 显示全部楼层
(vl-string-right-trim
  " "
  (vl-string-left-trim
    " "
    "     There are too many spaces here  "
  )
)
 楼主| 发表于 2009-10-9 01:11:00 | 显示全部楼层

看来还是自已搞错了,字前字后要去空格,中间也要去空格,下段文字:号前的空格也要去掉,A 01之间的空格需保留,关键字是":"号,(wcmatch "*:*"),现在不知道怎么写这段程序,愁!

A 01  :dskfjlfsjk

上段文字想要的结果是:

A 01:dskfjlfsjk

发表于 2009-10-9 08:02:00 | 显示全部楼层
(setq AA " A 01  :dskfjlfsjk ")
(setq AA (vl-string-right-trim
    " "
    (vl-string-left-trim
      " "
      AA
    )
  )
)
(setq STR
       (substr AA
        (1+ (setq POSITION (vl-string-position (ascii ":") AA)))
       )
)
(strcat (vl-string-right-trim
   " "
   (substr AA 1 (1- POSITION))
 )
 STR
)
 楼主| 发表于 2009-10-9 23:42:00 | 显示全部楼层
本帖最后由 作者 于 2009-10-10 0:45:29 编辑

龙大侠,上面的程序针对于":"前有空格的文字挺好用

A 01:dskfjlfsjk   如果":"前没有空格,会将":"前的1给删了!!!

(substr AA 1 (1- POSITION))这句可能有问题,不晓得该怎么改了!

还有就是不区分大小写要用到什么函数,也就是要让 a01  =  A01

发表于 2009-10-10 07:59:00 | 显示全部楼层

(substr AA 1 (1- POSITION))==>(substr AA 1 POSITION)

不区分大小写???要看你在甚麼地方用  (strcase "a01")="A01"

 楼主| 发表于 2009-10-10 08:20:00 | 显示全部楼层
測試通過了﹐謝謝龍大俠!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-11-6 07:06 , Processed in 0.179479 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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