Substituting special character / in string

Substituting special character / in string

Anonymous
Not applicable
2,022 Views
10 Replies
Message 1 of 11

Substituting special character / in string

Anonymous
Not applicable

After running: (setq fl (open "c:/braintree/MapName.txt" "r")
line (read-line fl)...

Variable "line" is : "K:\\TOB\\GIS\\MAPS (BASE ETC)\\13YrRoadConstruction.dwg"

I need to substitue "K" with "SERVER" and every instance of "\\" with "\" so it looks like:

("SERVER:\TOB\GIS\\MAPS (BASE ETC)\13YrRoadConstruction.dwg")

Unfortunately "\" is a special character which requires special attention. It's the special attention that I just can't figure out. I've tried various substitute routines including Lee Mac's StringSubst but the "\" buggers them all.

Any help would be appreciated.

Rod

Any 

0 Likes
2,023 Views
10 Replies
Replies (10)
Message 2 of 11

hak_vz
Advisor
Advisor

https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/lisp-replacing-backslash-with-forwar... 

 

Do you really need to replace "\\" with "\"?

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 3 of 11

Sea-Haven
Mentor
Mentor

Direct server name is \\\\ so returns server\\ rest of directories \\ is ok just server name needs more backslash's.

0 Likes
Message 4 of 11

john.uhden
Mentor
Mentor

You must understand that "\" is a special character in AutoCAD.  It means to treat the following character literally.   AutoCAD requires two (2) backlashes to represent one (1) backslash.  Alternatively, you can use one forwardslash in AutoCAD.

Now I've confused myself, but I think that in a world outside AutoCAD, you want to refer to the path as "\\server" so in AutoCAD you need to refer to the path as "\\\\server."

Give it a try.

John F. Uhden

0 Likes
Message 5 of 11

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

After running: (setq fl (open "c:/braintree/MapName.txt" "r")
line (read-line fl)...

Variable "line" is : "K:\\TOB\\GIS\\MAPS (BASE ETC)\\13YrRoadConstruction.dwg"

I need to substitue .... every instance of "\\" with "\" so it looks like:

("SERVER:\TOB\GIS\\MAPS (BASE ETC)\13YrRoadConstruction.dwg")

Unfortunately "\" is a special character which requires special attention. It's the special attention that I just can't figure out. .... 


It's a special character that displays in AutoLisp returned values as a double backslash as @john.uhden already mentioned, but it's not really a double one.  I reiterate @hak_vz 's question -- do you really need to change it?

 

It's coming from a file that already exists with a line with single backslashes in that file, though it displays when brought into AutoCAD with double ones.  Depending on what you want to do with it, you probably don't need to change it.  I did an experiment, making a little junk text file whose contents look like a filepath, with single backslashes:

Kent1Cooper_0-1609419306880.png

Then I did this:

Command: (setq file (open "C:\\temp\\junk.txt" "r")); open that file to read from
#<file "C:\\temp\\junk.txt">
Command: (setq file2 (open "C:\\temp\\junk2.txt" "w")); open another to write to
#<file "C:\\temp\\junk2.txt">
Command: (setq line (read-line file)); read the first file's first [only] line:
"C:\\one\\two\\three" {note the displayed-as-double backslashes, though they're single in the file}
Command: (write-line line file2); write that line to the other file
"C:\\one\\two\\three"
Command: (close file)
nil
Command: (close file2)
nil

 

Then the contents of the other file are:

Kent1Cooper_1-1609419534213.png

the same as in the first file, with the single backslashes, even though all "transactions" in AutoCAD displayed them as double. 

 

Then I did this:

  (command "_.text" "_mc" (getvar 'viewctr) "" "" line)

and the result in the middle of the screen was:

Kent1Cooper_0-1609420379959.png

with the single backslashes!  So actual usage as a text string does not have them doubled.

 

So do you really need to convert the displayed-as-but-not-really-double backslashes to single ones?  What are you doing with the filepath string?

Kent Cooper, AIA
Message 6 of 11

john.uhden
Mentor
Mentor

@Anonymous 

Study this carefully.  You may learn something from it...

Command: (setq str "c:\\temp")
"c:\\temp"

Command: (strlen str) 7

Command: (substr str 1 1) "c"

Command: (substr str 2 1) ":"

Command: (substr str 3 1) "\\"

Command: (substr str 4 1) "t"

Command: (setq str "c:\temp")
"c:\temp"

Command: (strlen str) 6

Command: (substr str 3 1) "\t"

John F. Uhden

0 Likes
Message 7 of 11

marko_ribar
Advisor
Advisor

@john.uhden 

I don't understand your gibberish info in you signature... Can you see to fix it to something prettier...

Thanks...

Marko Ribar, d.i.a. (graduated engineer of architecture)
0 Likes
Message 8 of 11

john.uhden
Mentor
Mentor
Marko,
That's supposed to be the Expert Elite thingy. Jeff Mischler sent me
directions, but my html skills are no better than my ability to translate
Sanskrit.

John F. Uhden

0 Likes
Message 9 of 11

Sea-Haven
Mentor
Mentor

To john I agree not sure what its meant to be.

 

Like my image I can walk on water just need 40mph. But it never helps me answering sometimes.

0 Likes
Message 10 of 11

Kent1Cooper
Consultant
Consultant

@marko_ribar wrote:

@john.uhden 

I don't understand your gibberish info in you signature... Can you see to fix it to something prettier...

and

@john.uhden  wrote:

That's supposed to be the Expert Elite thingy. ....


[Just so people aren't confused by those -- no gibberish now, and it is the proper thingy -- I provided something from the format of my signature to John that enabled him to fix his signature.]

Kent Cooper, AIA
0 Likes
Message 11 of 11

john.uhden
Mentor
Mentor

@marko_ribar :

Kent is my new savior; he showed me exactly how to fix it.  Yay!

(And I didn't have to translate anything from a dead language)

John F. Uhden

0 Likes