Jump to content

How can I translate \ to \\


ehsantavassolian

Recommended Posts

i want to translate \ to \\

For Example translate

"\\Pc1\source"

to

"\\\\Pc1\\source"

 

I wrote this

(vl-string-translate "\" "\\" "\\Pc1\source")

But there is a problem

The system does not accept "\"

Link to comment
Share on other sites

A single backslash in AutoLISP is an Escape Character, used to give an alternative meaning to characters which follow it (e.g. "\t" is the tab character). Therefore, before you pass the input string to an AutoLISP expression, you will first need to correctly represent the backslashes as literal backslashes within the string:

_$ (setq str "\\\\Pc1\\source")
"\\\\Pc1\\source"

Note that this will be printed with half the number of backslashes:

_$ (progn (princ str)(princ))
\\Pc1\source

Now, if you really wish to double the number of backslashes in the output string, there are a number of ways to accomplish this - here is one way:

_$ (vl-string-trim "\"" (vl-prin1-to-string str))
"\\\\\\\\Pc1\\\\source"

This output string will then be printed:

_$ (progn (princ (vl-string-trim "\"" (vl-prin1-to-string str)))(princ))
\\\\Pc1\\source

Link to comment
Share on other sites

The string "\s" is 1 char length. You can check it.

(strlen "\s") -> 1
(substr "\s" 1) -> "s"

It is impossible to even set a variable to your source string.

So first thing is to check your variable. Try both (princ my_var) and (prin1 my_var), the real content of the variable is what the prin1 returns.

Link to comment
Share on other sites

Thanks for your description

I learned your training

I tried a lot . But it's not working properly on this example

 

\\Pc1\source

 

Intended character is deleted

 

\\\\Pc1source

:cry:

Link to comment
Share on other sites

Thanks for your description

I learned your training

I tried a lot . But it's not working properly on this example

 

\\Pc1\source

 

Intended character is deleted

 

\\\\Pc1source

:cry:

 

There are only 2 ways you can enter this string :

 

 (setq path "\\\\Pc1\\source") 

or

 (setq path "//Pc1/source") 

even if your path is read from a (text)file lisp would automaticly translate "\\Pc1\source" to "\\\\Pc1\\source" so if you yourself enter this path manually you must use this string as shown above in your lisp code. If you let a user enter the path through a getstring you would get something like this :

 

Command: (setq path (getstring "\nPath : "))

Path : \\pc1\source

"\\\\pc1\\source"

 

 

gr. Rlx

Link to comment
Share on other sites

I tried a lot . But it's not working properly on this example

 

\\Pc1\source

 

Intended character is deleted

 

\\\\Pc1source

 

The backslash character is not 'deleted' as it was never there in the first place - per my post above, a single backslash in AutoLISP is an Escape Character. If you want to represent a backslash in an AutoLISP string, you need to prefix it with another backslash to mark it as a literal backslash and not an Escape Character.

 

Please re-read my post above.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...