Delete text before or after first space in a string

Delete text before or after first space in a string

kenneth.smithV6QLC
Enthusiast Enthusiast
3,709 Views
8 Replies
Message 1 of 9

Delete text before or after first space in a string

kenneth.smithV6QLC
Enthusiast
Enthusiast

I am looking for a LISP routine that can remove all text before or after a specific character in multiple text strings. I have found similar routines for block names but none yet for text strings. I have several dozen strings such as "12 FUSE, CARTRIDGE, 5 AMP" and "1 ASSEMBLY, POWER CONTROL" where the numbers at the start are item quantity. I want to separate the number and later text from eachother to make two strings.

 

Long explanation short: looking for 2 strings from 1. (1) just the numbers at the start of the string and (2) everything else.

 

So far the closest found:

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

https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/split-text-and-create-two-individual...

 

But I have to use text2mtxt on each and every line (which will eat up a lot of time).

 

Any help on this is appreciated greatly.

Please be kind, rewind your exploding. Do not explode hatching, dimensions or text. Think of your fellow drafters! 🙂

https://www.youracclaim.com/badges/9aa4a1d5-0d02-4888-be6f-ed8f0653d8ad/public_url
0 Likes
Accepted solutions (1)
3,710 Views
8 Replies
Replies (8)
Message 2 of 9

ronjonp
Mentor
Mentor

Here is a simple example how to split a string using a delimiter. Use 'T' as the last argument to chop the front and use 'NIL' to chop the back:

(defun _chop (str del f / i)
  (if (setq i (vl-string-search del str))
    (if	f
      (substr str (+ 1 (strlen del) i))
      (substr str 1 i)
    )
    str
  )
)
;; Chop front using 'T'
(_chop "12 FUSE, CARTRIDGE, 5 AMP" " " t)
;; "FUSE, CARTRIDGE, 5 AMP"
;; Chop back using 'nil'
(_chop "12 FUSE, CARTRIDGE, 5 AMP" " " nil)
;; "12" 
0 Likes
Message 3 of 9

Kent1Cooper
Consultant
Consultant

(setq YourString "12 FUSE, CARTRIDGE, 5 AMP")

Then:

(substr YourString 1 (vl-string-search " " YourString))
"12"

(substr YourString (+ 2 (vl-string-search " " YourString)))
"FUSE, CARTRIDGE, 5 AMP"

 

Those entries of

(vl-string-search " " YourString)

can be replaced, if you prefer, with

(vl-string-position 32 YourString)

 

If they're always just numbers at the beginning, you can also do the second one this way:

 

(vl-string-left-trim " 0123456789" YourString)
"FUSE, CARTRIDGE, 5 AMP"

 

but that approach won't do for isolating the number at the beginning.

Kent Cooper, AIA
Message 4 of 9

hak_vz
Advisor
Advisor

Try this

 

(defun c:rem_first_last ( / str_to_lst te tel str_lst str )
(defun str_to_lst (str / len lst pos )
    (setq len (1+ (strlen " ")))
    (while (setq pos (vl-string-search " " str))
        (setq lst (cons (substr str 1 pos) lst)
              str (substr str (+ pos len))
        )
    )
    (reverse (cons str lst))
)
(defun *error* ()(princ))
(setq te (car(entsel "\nSelect text to trim first and last word >")))
(setq tel (entget te))
(setq str_lst (str_to_lst (cdr (assoc 1 tel))))
(setq str_lst (reverse(cdr (reverse (cdr str_lst)))))
; Tony Tanzillo 
(setq str(apply 'strcat(cons (car str_lst) (mapcar '(lambda (s) (strcat " " s))(cdr str_lst)))))
(setq tel (subst (cons 1 str) (assoc 1 tel) tel))
(entmod tel)
(princ)
)

 Now you have to reshape this according to what you need i.e. reshaping all textt from selection, file or what ever.

 

This removes first and last element , but edit how you need - one cdr less 🙂

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 5 of 9

kenneth.smithV6QLC
Enthusiast
Enthusiast

Yes, they are always numbers at the beginning, no greater than 99 (so two characters) 🙂

I've gotten your code to work with removing the number, but can it include deleting the text afterwards? (leaving two text objects, one with the text and the other with the number?)

Please be kind, rewind your exploding. Do not explode hatching, dimensions or text. Think of your fellow drafters! 🙂

https://www.youracclaim.com/badges/9aa4a1d5-0d02-4888-be6f-ed8f0653d8ad/public_url
0 Likes
Message 6 of 9

ronjonp
Mentor
Mentor
Accepted solution

If you look at the example I posted above it includes how to get the text before and after the delimiter. Here is a consolidated version that will return the two text items in a list:

(defun _chop (str del / i)
  (if (setq i (vl-string-search del str))
    (list (substr str 1 i) (substr str (+ 1 (strlen del) i)))
  )
)
;; Chop
(_chop "12 FUSE, CARTRIDGE, 5 AMP" " ")
;; Returns ("12" "FUSE, CARTRIDGE, 5 AMP") 

Do you know how to access these items after they are returned as a list?

 

Message 7 of 9

kenneth.smithV6QLC
Enthusiast
Enthusiast

Overlooked that the first time around, that's exactly what I needed ^.^ Thank you!

Please be kind, rewind your exploding. Do not explode hatching, dimensions or text. Think of your fellow drafters! 🙂

https://www.youracclaim.com/badges/9aa4a1d5-0d02-4888-be6f-ed8f0653d8ad/public_url
0 Likes
Message 8 of 9

ronjonp
Mentor
Mentor

Glad to help 🙂

0 Likes
Message 9 of 9

Kent1Cooper
Consultant
Consultant

@kenneth.smithV6QLC wrote:

Yes, they are always numbers at the beginning, no greater than 99 (so two characters) 🙂

I've gotten your code to work with removing the number, but can it include deleting the text afterwards? (leaving two text objects, one with the text and the other with the number?)


[The way it's written, the number can be any  number of characters -- it's just looking for the space to break it around.]

 

The text string content for the two Text objects are from the combination of the two functions following "Then:" in my first reply, here for example putting them into variables:


  (setq YourString "12 FUSE, CARTRIDGE, 5 AMP")

Then:

  (setq

    number (substr YourString 1 (vl-string-search " " YourString))
    remainder (substr YourString (+ 2 (vl-string-search " " YourString)))

  )

 

You can apply those as content to Text strings, or Attributes, or whatever....

 

Just for fun, here's another cockamamie way to extract the number part at the beginning:

 

  (while

    (<

      47

      (ascii (setq char (substr YourString (setq n (1+ (cond (n) (0)))) 1)))

      58

    ); <

    (setq num (strcat (cond (num) ("")) char))

  )

 

provided the char, n and num variables are all nil initially.  It's stepping through the text content as long as each character it looks at is between 0 and 9, and will stop when it reaches the space.  The num variable ends up containing "12" from that sample string.  Again, the beginning number can be any number of characters.

Kent Cooper, AIA
0 Likes