Visual LISP, AutoLISP and General Customization

Visual LISP, AutoLISP and General Customization

Reply
Distinguished Contributor
ThomasGlidewell
Posts: 147
Registered: ‎10-03-2006
Message 1 of 7 (443 Views)

Remove a portion of a string

443 Views, 6 Replies
02-22-2012 02:19 PM

I am trying to make this: L:\Engineering\0208037\SURVEY\DRAWINGS turn into this: L:\Engineering\0208037\DRAWINGS

 

I am using the dwgprefix and it returns the first string and i want to make it in the second string as a new variable or replace the old variable so i can use it in a save as routine i am working on.


The problem is sometimes them project number (0208037) will be longer or shorter so i can't use the substr command

 

2012 all sp's.

and thanks to you all.

Valued Mentor
Shneuph
Posts: 352
Registered: ‎11-26-2007
Message 2 of 7 (430 Views)

Re: Remove a portion of a string

02-22-2012 04:23 PM in reply to: ThomasGlidewell

The VL-string... commands should give you everything that you need to accomplish this.. Check them out in the Developer Help VisualLISP reference.  They're very useful when working with strings.  Just remember (vl-load-com).  If you need further help I'll come up with something when I get a chance.

---sig---------------------------------------
'(83 104 110 101 117 112 104 64 71 109 97 105 108 46 99 111 109)
Valued Mentor
Shneuph
Posts: 352
Registered: ‎11-26-2007
Message 3 of 7 (426 Views)

Re: Remove a portion of a string

02-22-2012 04:58 PM in reply to: ThomasGlidewell

Saveapath original value:  "L:\\Engineering\\0208037\\SURVEY\\DRAWINGS"

via

(setq savepath (strcat (getvar "dwgprefix") (getvar "dwgname")))

 

 

Savepath new value: "L:\\Engineering\\0208037\\DRAWINGS"

via

(setq savepath (strcat (substr savepath 1 (vl-string-search "SURVEY" savepath)) (substr savepath (+ (vl-string-search "SURVEY" savepath) 8))))

---sig---------------------------------------
'(83 104 110 101 117 112 104 64 71 109 97 105 108 46 99 111 109)
*Expert Elite*
pbejse
Posts: 2,498
Registered: ‎11-24-2009
Message 4 of 7 (400 Views)

Re: Remove a portion of a string

02-23-2012 03:56 AM in reply to: ThomasGlidewell

ThomasGlidewell wrote:

I am trying to make this: L:\Engineering\0208037\SURVEY\DRAWINGS turn into this: L:\Engineering\0208037\DRAWINGS

 

I am using the dwgprefix and it returns the first string and i want to make it in the second string as a new variable or replace the old variable so i can use it in a save as routine i am working on.


The problem is sometimes them project number (0208037) will be longer or shorter so i can't use the substr command

 

2012 all sp's.

and thanks to you all.


We could have easily use Vl-REMOVE, but in case you had a folder with the same name along the filepath like this

"L:\\CAD\\Projects\\2012\\Project CodeXXX\\CAD\\"

 

the vl-remove function tends to remove duplicates,

same goes with vl-string-search method

 

(vl-string-search "CAD" "L:\\CAD\\Projects\\2012\\Project CodeXXX\\CAD\\") 3

 

If the variable was indeed from DWGPREFIX system variable (which includes "\\" at the end, unlike your example string)

 

So using a list and a number instead of a string name representing the position of the folder name:

 

(defun rempath  (str num / pref v p f str rp i pathlist)
      (setq pref (substr str 1 3))
      (while (and (setq v (vl-string-position 92 str))
                  (setq f (vl-string-position
                                92
                                (substr str (setq v (+ 2 v))))))
            (setq p   (cons (substr str v (1+ f)) p)
                  str (substr str v))
            )
      (setq rp (nth num (setq pathlist (cons pref (reverse p))))
            i  -1)
      (apply 'strcat
             (vl-remove-if
                   '(lambda (j)
                          (setq i (1+ i))
                          (and (equal j rp)
                               (= i num)
                               ))
                   pathlist)
             )
      )

 

(getvar 'dwgprefix)

"L:\\Engineering\\0208037\\SURVEY\\DRAWINGS\\"

 

(REMPATH (getvar 'dwgprefix) 3);<--- 3rd folder

"L:\\Engineering\\0208037\\DRAWINGS\\"

 

(REMPATH (getvar 'dwgprefix) 2);<--- 2nd folder

"L:\\Engineering\\SURVEY\\DRAWINGS\\"

 

HTH

Valued Mentor
Ian_Bryant
Posts: 362
Registered: ‎06-02-2005
Message 5 of 7 (387 Views)

Re: Remove a portion of a string

02-23-2012 11:39 AM in reply to: ThomasGlidewell
You could try
(vl-string-subst "" "\\SURVEY" (getvar "dwgprefix"))

Ian
Valued Mentor
Ian_Bryant
Posts: 362
Registered: ‎06-02-2005
Message 6 of 7 (358 Views)

Re: Remove a portion of a string

02-24-2012 12:26 AM in reply to: ThomasGlidewell
or perhaps more specific
(vl-string-subst "\\" "\\SURVEY\\" (getvar "dwgprefix"))
*Expert Elite*
pbejse
Posts: 2,498
Registered: ‎11-24-2009
Message 7 of 7 (357 Views)

Re: Remove a portion of a string

02-24-2012 12:43 AM in reply to: Ian_Bryant

Ian_Bryant wrote:
You could try
(vl-string-subst "" "\\SURVEY" (getvar "dwgprefix"))

Ian


What if:

 

(vl-string-subst "" "\\CAD" "L:\\CAD\\Projects\\2012\\Project CodeXXX\\CAD\\Current")

"L:\\Projects\\2012\\Project CodeXXX\\CAD\\Current"

 

Thats why i posted a  generic sub.

No string name, sequence dependent:

 

But thats just me :smileywink:

 

 

 

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.