samifox 发表于 2022-7-5 23:24:35

替换文字(无vlisp)

你好
 
我在练习字符串处理,但我遇到了一个错误,
红色标记的错误函数
希望得到一些帮助,
 

(defun C:test (/ str owrd nwrd i w fst lst)
(setq str (getstring T "\nPlease type your sentense:"))
(setq owrd (getstring T "\nType the word you want to replace"))
(setq nwrd (getstring T "\nType the word you want to replace with"))

(if (wcmatch str (strcat "*" owrd "*"))
   (progn
   (princ "\nMatch found")
   (setq i 1)
   (repeat (strlen str)
(setq w (substr str i (strlen owrd)))

(if (equal w owrd)
(progn
    (setq fst (subst str 1 i))
    (setq lst (subst str
                     (+ i (strlen owrd) 1)
                     (- (strlen str) i (strlen owrd))
              )
    )

    (princ (strcat fst nwrd lst))
)
)
(setq i (1+ i))
   )
   )
   (princ "\nMatch not found")
)
(princ)
)

 
谢谢
谢伊

pBe 发表于 2022-7-5 23:29:16

subst-->substr

samifox 发表于 2022-7-5 23:32:35

我真是个傻瓜
谢谢
 
Patrick,你怎么写这个函数?

pBe 发表于 2022-7-5 23:39:24

我现在没有cad samifox。
 
我想我也会这么做。但我不会使用(repeat str)一方面,它可以保证目标owrd通过wcmatch存在于str变量上,这只是找到确切位置的问题,它可能接近开始,所以不需要检查其余部分。使用while函数在找到目标str后立即终止评估。

Tharwat 发表于 2022-7-5 23:41:13

试试这个vl函数
 
(vl-string-translate "s" "m" "soon")

samifox 发表于 2022-7-5 23:45:08

我想这将是我在纯autolisp中的下一次练习

Tharwat 发表于 2022-7-5 23:48:21

 
祝你好运。

hmsilva 发表于 2022-7-5 23:52:55

替换字符串中同一单词的一个或多个实例,可能是这样的

(defun c:test (/ I NSTRL NWRD OSTRL OWRD STR STRBASE)
(if
   (and (setq str (getstring T "\nPlease type your sentense: "))
(not (= str ""))
(setq owrd (getstring T "\nType the word you want to replace: "))
(not (= owrd ""))
(setq nwrd (getstring T "\nType the word you want to replace with: "))
(wcmatch str (strcat "*" owrd "*"))
   );; and
    (progn
      (setq nstrl (strlen nwrd)
   ostrl (strlen owrd)
   i    1
      );; setq
      (while (= ostrl (strlen (setq strbase (substr str i ostrl))))
(if (= strbase owrd)
   (setq str (strcat (substr str 1 (1- i)) nwrd (substr str (+ i ostrl))))
);; if
(setq i
(if (= strbase owrd)
    (+ i nstrl)
    (1+ i)
);; if
);; setq
      );; while
      (prompt (strcat "\nNew string is: " str))
    );; progn
    (princ "\nMatch not found! ")
);; if
(princ)
);; test

HTH公司
亨里克

Tharwat 发表于 2022-7-5 23:56:59

getstring+Enter=“”不是零

hmsilva 发表于 2022-7-6 00:02:04

接得好,塔瓦
 
#8处的代码已编辑
页: [1] 2
查看完整版本: 替换文字(无vlisp)