TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: mgreven on February 24, 2012, 03:29:40 AM

Title: Remove double backslash from string?
Post by: mgreven on February 24, 2012, 03:29:40 AM
Hello,

Maybe i ask a stupid question, but is there a way to replace double backslashes in a string?

I am getting a result from the Filefind routine "C:\\TEMP\\temp.txt" which i want to transform into "C:\TEMP\temp.txt".
I tried it with the vl-string-subst command but i cannot give a single "\" to the routine?

Regards,

Marco
Title: Re: Remove double backslash from string?
Post by: Lee Mac on February 24, 2012, 05:30:04 AM
In LISP (and other languages), the backslash is used as an Escape Character (http://en.wikipedia.org/wiki/Escape_character) to give a sequence of characters an alternative meaning, e.g.:

Code: [Select]
"\n" = new line character
"\t" = tab character
"\r" = carriage return
...

Hence, to display a literal backslash you need to mark it as a literal by prefixing it with another backslash ("\\").

This means that a double backslash in LISP is interpreted as a single backslash.

Consider this example program:

Code - Auto/Visual Lisp: [Select]
  1. (defun c:test ( / str )
  2.     (setq str (getstring t "\nEnter a String with some Backslashes: "))
  3.     (princ "\nString in LISP format: ")
  4.     (prin1 str)
  5.  
  6.     (princ "\nString when printed:    ")
  7.     (princ str)
  8.     (princ)
  9. )

To demonstrate:

Code: [Select]
Command: test

Enter a String with some Backslashes: a\b\c

String in LISP format: "a\\b\\c"
String when printed:   a\b\c
Title: Re: Remove double backslash from string?
Post by: mgreven on February 25, 2012, 02:07:49 AM
Thanks for your respons...