Removing control Characters from a variable

Removing control Characters from a variable

Stuboy
Enthusiast Enthusiast
1,103 Views
6 Replies
Message 1 of 7

Removing control Characters from a variable

Stuboy
Enthusiast
Enthusiast

Hi,

 

I have created a pdf tool to automate plotting pdf's (setting ctb, page size etc) this also includes naming the pdf where I used someone else's code to return the text string from two attributes to create the a variable "filename" (drawing no and revision tags). this works great so long as the drawing number doesn't have control characters.

The drawingno tags input is RW0000-00/C.C02/0001 

 

I want it to take all the letters and numbers removing control characters so it becomes RW000000C020001 

 

I have managed to find this code (while (vl-string-search "/" filename) (setq filename (vl-string-subst "" "/" filename))) Which has removed the "/" how do I can I add the "-" and "." to this code.

 

Thanks in advance

 

SB

 

 

 

0 Likes
Accepted solutions (1)
1,104 Views
6 Replies
Replies (6)
Message 2 of 7

Ranjit_Singh
Advisor
Advisor

One example

(setq dat (mapcar 'ascii '("-" "." "/")))
(vl-list->string (vl-remove-if '(lambda (x) (member x dat)) (vl-string->list "RW0000-00/C.C02/0001")))
0 Likes
Message 3 of 7

devitg
Advisor
Advisor
Accepted solution

try it , rude as a flintstone work 

 

 

(setq filename "RW0000-00/C.C02/0001") 


 


(setq esp#-list ( list "?" "_" "-" "." "," "/"))

(setq filename
(foreach esp# ESP#-LIST 

  (while (vl-string-search esp# filename) (setq filename (vl-string-subst "" esp# filename)))
filename
  
  )
)
                  
 
0 Likes
Message 4 of 7

Stuboy
Enthusiast
Enthusiast

perfect devitg thanks legend

0 Likes
Message 5 of 7

Stuboy
Enthusiast
Enthusiast

thanks mate

0 Likes
Message 6 of 7

Kent1Cooper
Consultant
Consultant

@Stuboy wrote:

 

....

The drawingno tags input is RW0000-00/C.C02/0001 

 

I want it to take all the letters and numbers removing control characters so it becomes RW000000C020001 

 

.... 

Here's a way that doesn't remove just those particular three or six characters or any group of characters explicitly spelled out, but takes out anything  that's not a letter or number:

 

(setq newname ""); initially empty string

(foreach char (vl-string->list filename)

  (if (wcmatch (chr char) "@,#") (setq newname (strcat newname (chr char))))

); foreach

 

When applied to your example as the 'filename' variable, it returns

 

"RW000000CC020001"

 

[I assume you meant to have both  C's in there, despite one being missing from your sample result string.]

Kent Cooper, AIA
Message 7 of 7

Stuboy
Enthusiast
Enthusiast

Thanks Kent, always a wealth of knowledge, helped me out a lot over the years, this is code I can apply to a few lisps I have

0 Likes