LSP: Replace Last Character with User Specified Character

LSP: Replace Last Character with User Specified Character

jlaidle1
Advocate Advocate
3,217 Views
13 Replies
Message 1 of 14

LSP: Replace Last Character with User Specified Character

jlaidle1
Advocate
Advocate

Lisp needed to replace the last character in selected text objects, with a user specified text value?

 

John Laidler
ITO - Application Management


Please use "Accept as Solution" & give "Kudos" if this response helped you.

0 Likes
Accepted solutions (2)
3,218 Views
13 Replies
Replies (13)
Message 2 of 14

ronjonp
Mentor
Mentor

If it's just one character at the end this should do:

(setq str "MyText")
(setq newstr "Foo")
(strcat (substr str 1 (1- (strlen str))) newstr)

And the whole enchilada 🙂

(defun c:foo (/ a b s)
  (if
    (and (/= "" (setq a (getstring "\nEnter new string: "))) (setq s (ssget ":L" '((0 . "text")))))
     (foreach x	(vl-remove-if 'listp (mapcar 'cadr (ssnamex s)))
       (setq b (cdr (assoc 1 (entget x))))
       (entmod (append (entget x) (list (cons 1 (strcat (substr b 1 (1- (strlen b))) a)))))
     )
  )
  (princ)
)

 

0 Likes
Message 3 of 14

ВeekeeCZ
Consultant
Consultant
Accepted solution
(defun c:TextLastRep ( / s c i e x)

  (if (and (setq s (ssget "_:L" '((0 . "TEXT"))))
	   (setq c (getstring T "\nNew last char: "))
	   )
    (repeat (setq i (sslength s))
      (and (setq e (ssname s (setq i (1- i))))
	   (setq x (getpropertyvalue e "TextString"))
	   (setq x (substr x 1 (1- (strlen x))))
	   (setpropertyvalue e "TextString" (strcat x c)))))
  (princ)
  )
0 Likes
Message 4 of 14

jlaidle1
Advocate
Advocate

Works great!!!!!!  Can this also work for MTEXT objects?

Our users sometimes, for some reason, use MTEXT for single line text objects.

John Laidler
ITO - Application Management


Please use "Accept as Solution" & give "Kudos" if this response helped you.

0 Likes
Message 5 of 14

ronjonp
Mentor
Mentor

@jlaidle1 wrote:

Works great!!!!!!  Can this also work for MTEXT objects?

Our users sometimes, for some reason, use MTEXT for single line text objects.


THIS should do what you want as well .. not MTEXT though.

0 Likes
Message 6 of 14

ronjonp
Mentor
Mentor

Here's one that will do mtext too .. although the last character in an mtext string could be part of formatting so results could get a bit wonky.

(defun c:foo (/ a b o s)
  (and (/= "" (setq a (getstring "\nEnter new string: ")))
       (setq s (ssget ":L" '((0 . "*text"))))
       (foreach	x (vl-remove-if 'listp (mapcar 'cadr (ssnamex s)))
	 (setq b (vla-get-textstring (setq o (vlax-ename->vla-object x))))
	 (vla-put-textstring o (strcat (substr b 1 (1- (strlen b))) a))
       )
  )
  (princ)
)
(vl-load-com)
0 Likes
Message 7 of 14

ВeekeeCZ
Consultant
Consultant
Accepted solution

This will skip the formatted MTexts with info warning.

 

(defun c:TextLastRep ( / s c i p e x)

  (if (and (setq s (ssget "_:L" '((0 . "*TEXT"))))
	   (setq c (getstring T "\nNew last char: "))
	   )
    (repeat (setq i (sslength s))
      (and (setq e (ssname s (setq i (1- i))))
	   (setq p (if (= "MText" (getpropertyvalue e "LocalizedName"))
		     (cond ((= (getpropertyvalue e "Text")
			       (getpropertyvalue e "Contents"))
			    "Contents")
			   ((prompt (strcat "\n'" (getpropertyvalue e "Text") "' skipped due format chars: " (getpropertyvalue e "Contents")))))
		     "TextString"))
	   (setq x (getpropertyvalue e p))
	   (setq x (substr x 1 (1- (strlen x))))
	   (setpropertyvalue e p (strcat x c)))))
  (princ)
  )
Message 8 of 14

ronjonp
Mentor
Mentor

@jlaidle1  Not sure what to make of this thread. I've helped hundreds of people over the years and this is the first time I've been publicly ghosted. ( is that a thing now? )

 

Anyhoo ... if you're ever using an older version of CAD that does not include the get/setpropertyvalue functions included in @ВeekeeCZ 's code .. feel free to use mine as it should work.

 

2019-02-28_7-13-57.gif

 

 

0 Likes
Message 9 of 14

rozywahana
Observer
Observer

mr @ronjonp could u help me making lsp for string-replace

 

;; Example usage
(setq str "abcdeeeabcdeeeabcdeee")
(setq trimmed-str (string-replace str "e" ""))
;; trimmed-str will be "abcdabcdabcd"

 

0 Likes
Message 10 of 14

ronjonp
Mentor
Mentor

@rozywahana Try this:

(defun _removechar (ch str) (vl-list->string (vl-remove (ascii ch) (vl-string->list str))))
(setq str "abcdeeeabcdeeeabcdeee")
(_removechar "e" str)
;; "abcdabcdabcd" 
0 Likes
Message 11 of 14

rozywahana
Observer
Observer
mr @ronjonp can you write it in full code
Thank you in advance
0 Likes
Message 12 of 14

ronjonp
Mentor
Mentor

@rozywahana 

Full code HERE

0 Likes
Message 13 of 14

rozywahana
Observer
Observer

thanks before @ronjonp 

actually i mean lisp that can remove few character at front text like this,

before:

  A.jpg

 

After:

B.jpg

0 Likes
Message 14 of 14

ronjonp
Mentor
Mentor

I'll take a look tomorrow this is not nearly the same request a your initial

mr @ronjonp could u help me making lsp for string-replace
;; Example usage
(setq str "abcdeeeabcdeeeabcdeee")
(setq trimmed-str (string-replace str "e" ""))
;; trimmed-str will be "abcdabcdabcd"
0 Likes