@Tsuky Good Job. I did notice a bug however, and I am not sure why it is behaving like this. Try running your command on an MTEXT object that has only 1 field with no other text around it. For some reason it will kill the Field object, leaving behind "####" or "---".
EDIT: I figured it out - thanks to reviewing Lee Mac's FieldArithmetic program. You need to clear the text contents before re-adding them for some reason.
(defun c:foo ( / ss n ent obj old new)
(defun string-subst (nam_obj / value_string nbs tmp_nbs)
(setq value_string (vla-fieldcode nam_obj) nbs 0)
(while nbs
(if (setq nbs (vl-string-search "%pr0" value_string (setq tmp_nbs nbs)))
(setq
value_string (vl-string-subst "%pr2" "%pr0" value_string tmp_nbs)
nbs (1+ nbs)
)
)
)
(vlax-put nam_obj 'TextString "") ;; Add this line
(vlax-put nam_obj 'TextString value_string)
)
(setq ss (ssget '((0 . "MTEXT"))))
(cond
(ss
(repeat (setq n (sslength ss))
(setq
ent (ssname ss (setq n (1- n)))
obj (vlax-ename->vla-object ent)
)
(string-subst obj)
)
)
)
(prin1)
)