substring

Discussion in 'AutoCAD' started by Marcel Janmaat, Oct 12, 2004.

  1. I've written this to substitute a part of text within a string;

    (defun substring (txt old new / tel rep)
    (setq tel 1)
    (repeat (strlen txt)
    (if (= (substr txt tel (strlen old)) old)
    (setq rep (strcat (substr txt 1 (1- tel)) new
    (substr txt (+ tel (strlen old)) (- (strlen txt) (1- tel) (strlen old)))
    )
    txt rep
    )
    )
    (setq tel (1+ tel))
    )
    (if rep (eval rep) (eval txt))
    )

    Might there already be a command for this???

    M
     
    Marcel Janmaat, Oct 12, 2004
    #1
  2. Marcel Janmaat

    Jürg Menzi Guest

    Hi Marcel

    Do you know this?:
    Code:
    (defun MeSearchAndReplace (Stg Old New / TmpStr)
    (setq TmpStr Stg)
    (while (and
    (not (eq TmpStr ""))
    (vl-string-search Old TmpStr)
    )
    (setq TmpStr (vl-string-subst New Old TmpStr))
    )
    TmpStr
    )
    
    Cheers
     
    Jürg Menzi, Oct 12, 2004
    #2
  3. The FIND command (which has a Replace option) will do it.
     
    Kent Cooper, AIA, Oct 12, 2004
    #3
  4. Marcel Janmaat

    fengk Guest

    (MeSearchAndReplace "ABBC" "AB" "BA") returns "BBAC".
    But it should be "BABC".

    regards,
     
    fengk, Oct 13, 2004
    #4
  5. Marcel Janmaat

    Jürg Menzi Guest

    Hi fengk

    You're right, the correct code must be:
    Code:
    (defun MeSearchAndReplace (Stg Old New / StrLgt StrPos TmpStr)
    (setq TmpStr Stg
    StrLgt (strlen New)
    StrPos 0
    )
    (while (and
    (not (eq TmpStr ""))
    (setq StrPos (vl-string-search Old TmpStr StrPos))
    )
    (setq TmpStr (vl-string-subst New Old TmpStr StrPos)
    StrPos (+ StrPos StrLgt)
    )
    )
    TmpStr
    )
    
    Cheers
     
    Jürg Menzi, Oct 13, 2004
    #5
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Similar Threads
There are no similar threads yet.
Loading...