明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2322|回复: 12

有关字窜替代的问题

  [复制链接]
发表于 2004-11-17 09:21:00 | 显示全部楼层 |阅读模式
(vl-string-subst "to" "in" "go in the cinema in an hour ") 返回

"go to the cinema in an hour "

使用 vl-string-subst 仅替换字符串中第一次出现的子串,如果我想替换所有的子串怎么办?如:我想将“ABDAF”中的阿A都替换成B,成为“BBDBF”,有什么好办法?谢谢。

"觉得好,就打赏"
还没有人打赏,支持一下
发表于 2004-11-17 09:32:00 | 显示全部楼层
利用循环啊
发表于 2004-11-17 09:35:00 | 显示全部楼层
DosLIB or vl-string-translate _$ (dos_strreplace "ABDAF" "A" "B")
"BBDBF"
_$ (dos_strreplace "go in the cinema in an hour" "in" "to")
"go to the ctoema to an hour"
_$ (vl-string-translate "A" "B" "ABDAF")
"BBDBF"
_$
发表于 2004-11-17 09:38:00 | 显示全部楼层
(setq str "go in the cinema in an hour                 ")


(repeat(strlen str)


        (setq str        (vl-string-subst "to" "in" "go in the cinema in an hour                 ") )


)
发表于 2004-11-17 09:41:00 | 显示全部楼层
楼上的循环会出问题的
发表于 2004-11-17 10:41:00 | 显示全部楼层
_1$ (ST_ReplPattern "go in the cinema in an hour     " "in" "to" nil)
"go to the ctoema to an hour     "
  1. ;; ! ***************************************************************************
  2. ;; ! ST_ReplPattern
  3. ;; ! ***************************************************************************
  4. ;; ! Function : Replace a given pattern in a string with another pattern
  5. ;; ! Arguments : 'Str'           - String to check
  6. ;; !                         'Pat1'         - Pattern to search
  7. ;; !                         'Pat2'         - Pattern to replace
  8. ;; !                         'CaseSens' - If T, the search will be case-sensitive else no
  9. ;; ! Return     : The string after the pattern has been replaced.
  10. ;; !                       This is a case-sensitive search and replace
  11. ;; ! Updated   : April 23, 2004
  12. ;; ! e-mail     : rakesh.rao@4d-technologies.com
  13. ;; ! Web           : www.4d-technologies.com
  14. ;; ! ****************************************************************************(defun ST_ReplPattern   (Str Pat1 Pat2 CaseSens / newStr pos More len)
  15.    (setq
  16.        newStr ""
  17.        More     T
  18.        len       (strlen Pat1)
  19.        )   (if CaseSens
  20.        (setq _Str Str)
  21.        (setq
  22.            _Str (strcase Str)
  23.            Pat1 (strcase Pat1)
  24.            )
  25.        )   (while More
  26.        (setq pos (ST_FindPattern _Str Pat1))
  27.        (if pos
  28.            (progn
  29.                (setq
  30.                    newStr (strcat newStr (substr Str 1 (1- pos)) Pat2)
  31.                    Str       (substr Str (+ pos len))
  32.                    _Str     (substr _Str (+ pos len))
  33.                    )
  34.                (if (= Str "")
  35.                    (setq More nil)
  36.                    )
  37.                )
  38.            (setq
  39.                newStr (strcat newStr Str)
  40.                More     nil
  41.                ))
  42.        )
  43.    newStr
  44.    )
  45. ;; ! ***************************************************************************
  46. ;; ! ST_FindPattern
  47. ;; ! ***************************************************************************
  48. ;; ! Function : Returns the start of a given text pattern in a test string
  49. ;; ! Argument : 'Str' - Text String to check
  50. ;; !                       'Pat' - Pattern string to search
  51. ;; ! Return     : The starting position of the first pattern string, if pattern is
  52. ;; !                       found else returns nil.
  53. ;; ! Update     : September 21, 1999
  54. ;; ! e-mail     : rakesh.rao@4d-technologies.com
  55. ;; ! Web           : www.4d-technologies.com
  56. ;; ! ****************************************************************************(defun ST_FindPattern   (Str Pat / len1 len2 pos Found _Str)
  57.    (setq
  58.        len1 (strlen Str)
  59.        len2 (strlen Pat)
  60.        pos   nil
  61.        )   (if (>= len1 len1)
  62.        (progn
  63.            (setq
  64.                Found nil
  65.                pos     1
  66.                )
  67.            (while (not Found)
  68.                (setq _Str (substr Str pos len2))
  69.                (if (equal _Str Pat)
  70.                    (setq Found T)
  71.                    (setq pos (1+ pos))
  72.                    )
  73.                (if (> pos len1)
  74.                    (setq
  75.                        Found T
  76.                        pos nil
  77.                        )
  78.                    )
  79.                )
  80.            ))
  81.    pos
  82.    )
发表于 2004-11-17 13:35:00 | 显示全部楼层
用find命令不行么?
 楼主| 发表于 2004-11-17 13:47:00 | 显示全部楼层
(vl-string-subst "ll" "a" "abb") 返回llbb


(vl-string-translate         "a" "ll" "abb")返回lbb


vl-string-translate好像只能同等位数的字符的转化,我想要vl-string-subst 函数的效果,但是能够替换掉每个字窜。如:(函数 "ll" "abcab") 返回"llbcllb",有没有这样的函数?谢谢。
发表于 2004-11-17 14:08:00 | 显示全部楼层
_$ ST_ReplPattern "abcab" "a" "ll" nil)
nil
"llbcllb"
_$
 楼主| 发表于 2004-11-17 14:45:00 | 显示全部楼层
ok la. thank you very much.
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-9-30 06:23 , Processed in 0.179832 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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