Author Topic: Remove double backslash from string?  (Read 3952 times)

0 Members and 1 Guest are viewing this topic.

mgreven

  • Guest
Remove double backslash from string?
« 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

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: Remove double backslash from string?
« Reply #1 on: February 24, 2012, 05:30:04 AM »
In LISP (and other languages), the backslash is used as an 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
« Last Edit: February 24, 2012, 05:35:17 AM by Lee Mac »

mgreven

  • Guest
Re: Remove double backslash from string?
« Reply #2 on: February 25, 2012, 02:07:49 AM »
Thanks for your respons...