乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 22|回复: 16

[编程交流] 替换文字(无vlisp)

[复制链接]

218

主题

699

帖子

483

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1090
发表于 2022-7-5 23:24:35 | 显示全部楼层 |阅读模式
你好
 
我在练习字符串处理,但我遇到了一个错误,
红色标记的错误函数
希望得到一些帮助,
 
  1. (defun C:test (/ str owrd nwrd i w fst lst)
  2. (setq str (getstring T "\nPlease type your sentense:"))
  3. (setq owrd (getstring T "\nType the word you want to replace"))
  4. (setq nwrd (getstring T "\nType the word you want to replace with"))
  5. (if (wcmatch str (strcat "*" owrd "*"))
  6.    (progn
  7.      (princ "\nMatch found")
  8.      (setq i 1)
  9.      (repeat (strlen str)
  10. (setq w (substr str i (strlen owrd)))
  11. (if (equal w owrd)
  12.   (progn
  13.     (setq fst [color="red"][b](subst str 1 i)[/b][/color])
  14.     (setq lst (subst str
  15.                      (+ i (strlen owrd) 1)
  16.                      (- (strlen str) i (strlen owrd))
  17.               )
  18.     )
  19.     (princ (strcat fst nwrd lst))
  20.   )
  21. )
  22. (setq i (1+ i))
  23.      )
  24.    )
  25.    (princ "\nMatch not found")
  26. )
  27. (princ)
  28. )

 
谢谢
谢伊
回复

使用道具 举报

pBe

32

主题

2722

帖子

2666

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
211
发表于 2022-7-5 23:29:16 | 显示全部楼层
subst-->substr
回复

使用道具 举报

218

主题

699

帖子

483

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1090
发表于 2022-7-5 23:32:35 | 显示全部楼层
我真是个傻瓜
谢谢
 
Patrick,你怎么写这个函数?
回复

使用道具 举报

pBe

32

主题

2722

帖子

2666

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
211
发表于 2022-7-5 23:39:24 | 显示全部楼层
我现在没有cad samifox。
 
我想我也会这么做。但我不会使用(repeat str)一方面,它可以保证目标owrd通过wcmatch存在于str变量上,这只是找到确切位置的问题,它可能接近开始,所以不需要检查其余部分。使用while函数在找到目标str后立即终止评估。
回复

使用道具 举报

63

主题

6297

帖子

6283

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
358
发表于 2022-7-5 23:41:13 | 显示全部楼层
试试这个vl函数
 
  1. [color=red]([/color][color=blue]vl-string-translate[/color] [color=magenta]"s" "m" "soon"[/color][color=red])[/color]
回复

使用道具 举报

218

主题

699

帖子

483

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1090
发表于 2022-7-5 23:45:08 | 显示全部楼层
我想这将是我在纯autolisp中的下一次练习
回复

使用道具 举报

63

主题

6297

帖子

6283

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
358
发表于 2022-7-5 23:48:21 | 显示全部楼层
 
祝你好运。
回复

使用道具 举报

1

主题

475

帖子

481

银币

初来乍到

Rank: 1

铜币
5
发表于 2022-7-5 23:52:55 | 显示全部楼层
替换字符串中同一单词的一个或多个实例,可能是这样的
  1. (defun c:test (/ I NSTRL NWRD OSTRL OWRD STR STRBASE)
  2. (if
  3.    (and (setq str (getstring T "\nPlease type your sentense: "))
  4. (not (= str ""))
  5. (setq owrd (getstring T "\nType the word you want to replace: "))
  6. (not (= owrd ""))
  7. (setq nwrd (getstring T "\nType the word you want to replace with: "))
  8. (wcmatch str (strcat "*" owrd "*"))
  9.    );; and
  10.     (progn
  11.       (setq nstrl (strlen nwrd)
  12.      ostrl (strlen owrd)
  13.      i    1
  14.       );; setq
  15.       (while (= ostrl (strlen (setq strbase (substr str i ostrl))))
  16. (if (= strbase owrd)
  17.    (setq str (strcat (substr str 1 (1- i)) nwrd (substr str (+ i ostrl))))
  18. );; if
  19. (setq i
  20. (if (= strbase owrd)
  21.     (+ i nstrl)
  22.     (1+ i)
  23.   );; if
  24. );; setq
  25.       );; while
  26.       (prompt (strcat "\nNew string is: " str))
  27.     );; progn
  28.     (princ "\nMatch not found! ")
  29. );; if
  30. (princ)
  31. );; test

HTH公司
亨里克
回复

使用道具 举报

63

主题

6297

帖子

6283

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
358
发表于 2022-7-5 23:56:59 | 显示全部楼层
getstring+Enter=“”不是零
回复

使用道具 举报

1

主题

475

帖子

481

银币

初来乍到

Rank: 1

铜币
5
发表于 2022-7-6 00:02:04 | 显示全部楼层
接得好,塔瓦
 
#8处的代码已编辑
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-11 00:08 , Processed in 0.628113 second(s), 72 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表