Author Topic: Help with code to extract a string from text.  (Read 7845 times)

0 Members and 1 Guest are viewing this topic.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Help with code to extract a string from text.
« Reply #30 on: February 11, 2013, 10:29:48 PM »
Yes, very impressive Lee.  8-)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Help with code to extract a string from text.
« Reply #31 on: February 12, 2013, 07:16:29 AM »
Cheers guys  8-)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Help with code to extract a string from text.
« Reply #32 on: February 13, 2013, 10:26:40 AM »
Reu,
Your code was close but did not work correctly.
Revised code below:
Code: [Select]
;;; Function (C:MCHAR) ;
;;; Description: This program is to remove a number of characters from the end ;
;;; of a text string and place them at the beginning of a 2nd string. ;
(defun c:mchar (/ #chrs ss1 ss2 entlst1 entlst2 txt1 txt2 str)

  (and
    (princ "\nAppend characters from 1st text to the 2nd text.")
; Print message to tell user what the program does.
    (setq #chrs (getint "\nHow many characters do you want to move? "))
; Set variable "#chars" to user defined number of characters to move.
    (princ "\nSelect the 1st Text to get chars.")
; Print message to tell user what to do.
    (setq ss1 (ssget "_:S" '((0 . "TEXT"))))
; Set variable "ss1" to the user defined text object to move characters from.
    (setq entlst1 (entget (ssname ss1 0)))
; Set variable "entlst1" to the association list of the user defined text to move characters from.
    (setq txt1 (cdr (assoc 1 (entget (ssname ss1 0)))))
; Set variable "txt1" to the text string of the user defined text to move characters from.
    (princ "\nSelect the Text to add chars to.")
; Print message to tell the user what to do.
    (setq ss2 (ssget "_:S" '((0 . "TEXT"))))
; Set variable "ss2" to the user defined text object to move characters to.
    (setq entlst2 (entget (ssname ss2 0)))
; Set variable "entlst2" to the association list of the user defined text to move the characters to.
    (setq txt2 (cdr (assoc 1 (entget (ssname ss2 0)))))
; Set variable "txt2" to the text string of the user defined text to move characters from.
    (setq str (substr txt1 (1+ (- (strlen txt1) #chrs))))
; Set variable "str" to the text string to be moved from 1st text to 2nd text.

    (entmod ; update the following entity list
      (subst ; make some changes to the list "entlst2"
(cons 1 (strcat str " " (cdr (assoc 1 entlst2))))
; new string + 'space character' + dxf 1
(assoc 1 entlst2) ; old string with DXF code "1"
entlst2 ; original entity association list
      ) ;_end subst
    ) ;_end entmod
    (entmod
      (subst ; make some changes to the list "txt1"
(cons 1 (substr txt1 1 (- (strlen txt1) #chrs))) ;                               <-----<<<  CAB changed this
; "txt1" - ("#chars" + 3)
(assoc 1 entlst1) ; old string with DXF code "1"
entlst1 ; original entity association list
      ) ;_end subst
    ) ;_end entmod
  ) ;_end and
  (princ "\nType MCHAR to invoke.")
  (princ)
) ;_end defun
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Reu

  • Guest
Re: Help with code to extract a string from text.
« Reply #33 on: February 13, 2013, 10:32:13 AM »
Reu,
Your code was close but did not work correctly.
Revised code below:


Very good sir. Thank you.