how to trim spaces in a string

Discussion of MicroSurvey CAD related issues and questions.

Moderators: Brian Sloman, Jason Poitras, James Johnston

Post Reply
glendon
Posts: 9
Joined: Wed Jan 27, 2010 10:55 am

how to trim spaces in a string

Post by glendon » Thu Jan 28, 2010 1:04 pm

I tried to erase spaces in a string using LISP; the help system gives explanation as follows.
But 'trim' is always used for lines. I coded some lisp for MSCAD 2010, they all work well.

thanks


(trim string [flag])
--------------------------------------------------------------------------------
Erase blank spaces in a string.
--------------------------------------------------------------------------------

This function trims spaces from the specified string.

The optional flag argument represents the options for stripping spaces:


Flag Meaning
none Strip all spaces from beginning and end.
"B" Strip spaces from beginning of string.
"M" Strip spaces from middle of string.
"E" Strip spaces from end of string.
"BE" Strip spaces from beginning and end of string.
"ME" Strip spaces from middle and end of string.
"BM" Strip spaces from beginning and middle of string.

User avatar
Brian Sloman
Product Manager
Posts: 407
Joined: Fri Jan 07, 2005 4:42 pm
Location: Westbank, BC, Canada
Contact:

Re: how to trim spaces in a string

Post by Brian Sloman » Thu Jan 28, 2010 7:33 pm

Hi, what exactly are you trying to accomplish? The (trim) lisp command is completely unrelated to CAD, it works as documented to remove spaces from a string.

Code: Select all

: (trim "      string      ")
"string"
But the bigger question is how are you are getting the string in the first place, and what are you trying to do with it once trimmed? I'm guessing that you want to select text entities from the drawing, and this is probably where you're having the problems. Here's a function from a file i wrote showing how you can do that.

Code: Select all

(princ "\nLoading TRIMTEXT.LSP...")
;Trim spaces from selected text
(defun C:TRIMTEXT (/ ent entg txt)
  (while (setq ent (car (entsel "\nSelect text to trim: ")))
    (setq entg (entget ent))
    (if (= (cdr (assoc 0 entg)) "TEXT")
      (progn
        (setq txt (cdr (assoc 1 entg)))
        (setq txt (trim txt))
        (setq entg (subst (cons 1 txt) (assoc 1 entg) entg))
        (entmod entg)
      )
    )
  )
  (princ)
)
(princ "\nCommand -> TRIMTEXT")
(princ)
And of course you can add any of the optional flags to the trim command to get your desired results to trim just the beginning or end, etc. Ahhh it's fun playing programmer once in a while :) Hope this helps!
Brian Sloman
Development Manager
MicroSurvey Software Inc.

glendon
Posts: 9
Joined: Wed Jan 27, 2010 10:55 am

Re: how to trim spaces in a string

Post by glendon » Fri Jan 29, 2010 7:18 am

Thank you, Brian
Your codes work, I was wrong in selecting text.

Post Reply