Jump to content

lisp to replace a single character in a text string


jmseiver

Recommended Posts

Hello,

 

I haven't done Lisp programming in about ten years and need a quick fix. Would someone please help me get started with this requirement:?

 

lisp to select a single character in a text string and replace it with another character.

 

I know how to select the string and get it into the selection set SS.

 

Thank you in advance!

Link to comment
Share on other sites

I haven't done Lisp programming in about ten years and need a quick fix. Would someone please help me get started with this requirement:?

 

lisp to select a single character in a text string and replace it with another character.

 

I know how to select the string and get it into the selection set SS.

 

Welcome to CADTutor!

 

Since you're dealing with a selection set within your active document, have you considered simply using the FIND Command?

 

Cheers

Link to comment
Share on other sites

I have found this routine fantastic for both simple and complex 'find and replace' functions. I tried to get one to do specifics myself, and stumbled on this that did what I wanted and much more. You may not need to re-invent the wheel ;)

 

As usual... from the guru Lee Mac:

http://www.lee-mac.com/bfind.html

Link to comment
Share on other sites

I would like to use the lisp routine in a batch file.

 

That would have been useful information in the OP, as in a batch process using ObjectDBX, selection sets are not supported as they are in Scripts (because the drawings are not opened in the editor).

 

1+ for BFIND.

Link to comment
Share on other sites

Hello,

 

I haven't done Lisp programming in about ten years and need a quick fix. Would someone please help me get started with this requirement:?

 

lisp to select a single character in a text string and replace it with another character.

 

I know how to select the string and get it into the selection set SS.

 

Thank you in advance!

 

hi jmseiver, ithink below lisp will Solve your troubles, i write and test it in my computer, it works

 

;;;;<make sure your CAD express tools can be used>
;;;;design by shaodeng000 <take a hour>

(defun c:tt ()
 (acet-error-init
   (list (list	"cmdecho" 0
;;;               "highlight" "
	"osmode" 0 "limcheck" 0	"pickstyle" 0) ;list
  T
   )					;list
 )
 (setq	flt '((-4 . "<OR")
      (0 . "TEXT")
      (0 . "MTEXT")
      (-4 . "OR>")
     )
 );select strings
 (setq gop1 (ssget flt))
 (setq gop gop1)
 
 (if (not #str_x2)
     (setq #str_x2 "stringold")
 )
 (setq	str_x2 (getstring (acet-str-format
		    "\nenter ori string<%1>"
		    #str_x2
		  )
       )
 )
 (if (or (not str_x2)
  (equal str_x2 "")
     )	;or
   (setq str_x2 #str_x2)
   (setq #str_x2 str_x2)
 );set a original string that you want to replace.
 (if (not str_x1)
     (setq str_x1 "stringnew")
 );set a new string.
 (setq	str_x (getstring
	(acet-str-format "\n<%1>instead of<%2>"  str_x1 str_x2
	  );end acet-str-format	
      )
 )
 (if (or (not str_x)
  (equal str_x "")
     )					;or
   (setq str_x str_x1)
   (setq str_x1 str_x)
 )
 (change_str gop1 str_x1 str_x2)
 (acet-error-restore)
)

(defun change_str (gop str_new str_old / ent entname i)
 (setq i 0)
 (repeat (sslength gop)
   (setq entname (ssname gop i))
   (setq ent (entget entname))
   (setq strings (cdr (assoc 1 ent)))
   (setq str_change (vl-string-subst str_new str_old strings))
   (entmod (subst (cons 1 str_change) (assoc 1 ent) ent))
   (setq i (1+ i))
   )
 )

Link to comment
Share on other sites

Hello,

lisp to select a single character in a text string and replace it with another character.

 

command:FIND

or try function

;http://www.cadtutor.net/forum/showthread.php?88249-lisp-to-replace-a-single-character-in-a-text-string

(replace$ ss new old) 
;argument:
;ss   = selection set
;new = new string, 'STR
;old = old string to be replaced, 'STR

(defun replace$	(ss new old / str) ;hanhphuc 2014
 (if ss
   (mapcar ''((x)
       (while
	(and (setq str (assoc 1 (entget x))) (wcmatch (cdr str) (strcat "*" old "*")))
	(vla-put-textstring (vlax-ename->vla-object x) (vl-string-subst new old (cdr str)))
	)
       )
    (vl-remove-if ''((e) (listp e)) (mapcar 'cadr (ssnamex ss)))
    ) ;_ end of mapcar
   ) ;_ end of if
 ) ;_ end of defun

Test:

;note: case sensitive
(replace$ (ssget "X") "NEW" "OLD") 

please test which character can't be replaced?

example: "#"

Link to comment
Share on other sites

@ hanhphuc

 

Why do not you build the appropriate filtration codes per requirements to avoid any failure of codes ?

eg and as per your last posted codes here .

 

... '((0 . "*TEXT")(1 . "OLD"))

Link to comment
Share on other sites

@ hanhphuc

 

Why do not you build the appropriate filtration codes per requirements to avoid any failure of codes ?

eg and as per your last posted codes here .

 

... '((0 . "*TEXT")(1 . "OLD"))

 

Thank you sir for advise, appreciated.

yes, i know the above ssget is normal good practice as shown in post#6

i just wanna try something difference or variant

which i filter in a list (ssnamex) (assoc 1 ..) (wcmatch ..), but i still don't know known bugs

that's why argument i use ss to be tested by user

 

i believe you know more bugs far more than me because your experience is not a short cut, in a glance you can easily spot it. Debugging is a good lesson agree? :)

p/s: known bug: " " (blank) may be trapped in while loop

Edited by hanhphuc
Link to comment
Share on other sites

(vl-remove-if ''((e) (listp e))

 

Note that this could become simply:

(vl-remove-if 'listp ... )

Though, there is no need to iterate over the selection set data returned by ssnamex more than once, e.g.:

(defun replace$ ( sel new old / obj )
   (if sel
       (foreach itm (ssnamex sel)
           (and (= 'ename (type (cadr itm)))
                (setq obj (vlax-ename->vla-object (cadr itm)))
                (vla-put-textstring obj (LM:stringsubst new old (vla-get-textstring obj)))
           )
       )
   )
)

;; String Subst  -  Lee Mac
;; Substitutes a string for all occurrences of another string within a string.

(defun LM:stringsubst ( new old str / pos )
   (if (setq pos (vl-string-search (strcase old) (strcase str)))
       (strcat (substr str 1 pos) new (LM:stringsubst new old (substr str (+ 1 pos (strlen old)))))
       str
   )
)

Link to comment
Share on other sites

Note that this could become simply:

(vl-remove-if 'listp ... )

Though, there is no need to iterate over the selection set data returned by ssnamex more than once, e.g.:

 

yeah 3rd eyes always "sharper", thank you for spending your time to correct it, i noted for today's lesson :)

i also learned vl-string-search method from you similar to Lee's which i didn't notice, nice!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...