samifox 发表于 2022-7-6 00:04:09

 
这样地:
 

(defun C:test (/ str owrd nwrd i w fst lst)

(setq str (getstring T "\nPlease type your sentense:\n"))
(setq owrd (getstring T "\nType the word you want to replace\n"))
(setq nwrd (getstring T "\nType the word you want to replace with\n"))

(if (wcmatch str (strcat "*" owrd "*"))
   (progn
   (setq i 0)
   (setq w "")
   
   (while (not (equal w owrd))
(setq i (+ i 1))
(setq w (substr str i (strlen owrd)));_search for the old word


   )
(setq fst (substr str 1 (- i 1)))
(setq        lst (substr str
                  (+ i (strlen owrd))
                  (- (strlen str)(strlen owrd))
          )
)

(princ (strcat fst nwrd lst))
(setq w nil) ;_cuz while stop
) ;_progn if yes


   (princ "\nMatch not found\n")
   
)
(princ)
)

pBe 发表于 2022-7-6 00:06:49

较好的我在考虑运行一个循环来包含目标字符串的多个实例
 
试试这个:
(Defun c:repstr ( / _valid str owrd nwrd str nstr)
;;                pBe013March2014                ;;;
(defun _valid (msg / a)
(setq a (getstring T msg))
        (if (snvalid a) a (prompt "\n<<<Invalid String>>>")))
(and
(setq str (_valid "\nPlease type your sentense: "))
(setq owrd (_valid "\nType the word you want to replace: "))
(setq nwrd (_valid "\nType the word you want to replace with: "))
        (while (wcmatch str (strcat "*" owrd "*"))
          (setq        i    0
                nstr str
                l    (strlen owrd)
          )
                (While (eq str nstr)
                  (if (eq owrd (substr str (setq i (1+ i)) l))
                  (setq nstr (strcat (substr str 1 (setq n (1- i)))
                                     nwrd
                                     (substr str (+ n l 1))
                             )
                  )
                  )
                )
          (setq str nstr)
        )
(print str)
)
(princ)
)

irneb 发表于 2022-7-6 00:11:56

我的方法(如果不使用VL):
(defun str-subst-all (new old string / search return)
(setq search (strcat old "*") return "")
(while (not (eq string ""))
   (if (wcmatch string search)
   (setq string (substr string (1+ (strlen old))) return (strcat return new))
   (setq return (strcat return (substr string 1 1)) string (substr string 2))))
return)

irneb 发表于 2022-7-6 00:13:35

或者更优化一点,并结合类似于VL方法的内容:
(从(找到/len1 len2的模式字符串)中定义str位置)(if(wcmatch string(strcat“*”pattern“*”)(progn(setq len1(strlen模式)len2(strlen字符串)模式(strcat模式“*”))(while(和(未找到)(

pBe 发表于 2022-7-6 00:18:00

 
要么你的库中已经有了这些代码,要么你手头有太多的时间
 
很好的vl复制功能

irneb 发表于 2022-7-6 00:23:44

o: )其中一些我已经有了,例如看课文。此处为lsp文件:http://sourceforge.net/p/caddons/code/HEAD/tree/Libraries/ 
13号帖子中的一个问题是我在之前的帖子中提出的,虽然我记不清具体在哪里,但谷歌似乎帮不上什么忙。#14中的一个是一个快速重写,大约15分钟的编码和测试-我在发布#13后写的。
 
至于vl模仿者。。。在我看来,重新发明轮子不是一个好做法。如果已经有一个类似的函数,那么您的函数应该尝试遵循相同的参数和顺序(至少从外部看是这样)。否则,您的函数的用户会发现不一致,并且在使用您的函数时,他们对其他函数的了解变得无用。

pBe 发表于 2022-7-6 00:25:05

 
的确
页: 1 [2]
查看完整版本: 替换文字(无vlisp)