Why doesn't Julia recognize my string?

#1

Hey guys

So basically I have a string like this:

“D:\Test”

And when I use this in Julia v.1.1 it tells me it has an invalid escape sequence, which doesn’t make sense for me, since it should just read it as a string? Then I try using:

raw"D:\Test"

And now it works and does what I want, BUT I want to input “D:\Test” in a function and then turn it into raw"D:\Test", but I just can’t figure out how. It should be pretty simple I guess?

I use Windows if it matters.

Kind regards

0 Likes

#2

Just a guess but “D:\\Test”?

0 Likes

#3

Yes, D:\\Test works, but I wanted to do it programmatically, so I could parse a “wrong” string to a function and let it fix it for me. My temporary solution was to force the user to write “raw” infront of every string, which works but is kind of annoying.

Kind regards

0 Likes

#4

“D:\Test” is not a valid string and hence it can only be viewed as a sequence of bytes which can then be converted to a raw string.

1 Like

#5

If the user is interfacing with this function in Julia code, then that or escaping is the right solution.

If, instead, this is read from a prompt or a dialog box, then the string should be properly represented (as if you constructed it with raw), so you don’t need to do anything.

2 Likes

#6

I primarily come from Matlab, so this concept of “invalid” strings is quite new for me. How would I go about converting it to a sequence of bytes and modifying this sequence programmatically until I get what I want?

Kind regards

0 Likes

#7

I have not looked into dialog boxes etc. yet, but might be a valid solution. I just would like to be able to copy paste an address from file explorer and inserting into my function argument, and letting my function fix the mistake.

To do this, raw, seems to be the best option. I tried using the @raw_str functionality, but it cannot handle it unfortunately.

Kind regards

0 Likes

#8

\ is an escape sequence (https://en.wikipedia.org/wiki/Escape_sequences_in_C) which means that it together with the following character has a special meaning. For example, \n is a newline and \t is a tab. There is no \T and therefore you get an error. If you want the character \ it is written in a string as \\.

2 Likes

#9

Ah okay, thanks for explaining it, now I can see why it becomes a problem. @zgornel talked about storing it in a byte array of some kind, would I then be able to programmatically detect the “\”-byte and add an extra one and then turn it into a string? If yes, could anyone provide a small example? - my googling has not been too helpful…

Kind regards

0 Likes

#10

I don’t think there’s an easy way to paste a string into Julia code and interpret it as a byte array.

But if you’re trying to copy-paste a string to a REPL/Juno line for immediate use, you can replace the pasting step with the the built-in clipboard. That way, you don’t need to worry about escaping. For example, after copying D:\Test

julia> s = clipboard()
"D:\\Test"

julia> println(s)
D:\Test

1 Like

#11

Ah okay, that is also a somewhat viable solution. Thanks, for letting me know!

I still feel like it should be possible to make Julia “forget” its rules about escape sequences \n etc. if explicitly stated, but maybe this will affect performance more than I think. I am not sure, whom to select as a solution, since a lot of different suggestions which all kind of work to be fair.

Kind regards

0 Likes

#12

This is precisely what raw"" does. It parses your string exactly as written, with no attempt to apply escape sequences.

0 Likes

#13

I think Julia also handles unix-style paths on Windows computers, so if you write "c:/test", that should also work on a Windows machine. (Windows is not case sensitive when it comes to file names/paths, I believe.) So if it is less work, you can replace backslash (\) with slash (/). If you do copy and paste, that doesn’t help much, though.

0 Likes

#14

The \n are not stored in the string as \ followed by an n. It is one character.

julia> codeunits("\n")
1-element Base.CodeUnits{UInt8,String}:
 0x0a

You can make a string from bytes as

julia> String(UInt8[72, 101, 108, 108, 111])
"Hello"

But there is no byte (or sequence of bytes) representing \T because that character does not exist. If you want to insert the characters \ follwed by a T it is:

julia> codeunits("\\T")
2-element Base.CodeUnits{UInt8,String}:
 0x5c
 0x54
3 Likes

#15

It’s true that Matlab literal strings entered directly don’t have backslash escapes. The downside of this is that you cannot directly enter a Matlab literal that has a newline character and similar (e.g. arbitrary Unicode codepoints), since you can’t use \n etcetera.

The workaround in Matlab is to use sprintf, which provides backslash escapes, but then there are invalid escapes:

>> sprintf('D:\Test')   
Warning: Control Character '\T' is not valid. See 'doc sprintf' for control
characters valid in the format string. 

ans =

D:

This is a matter of taste, but if you work enough with strings I think most people find having escapes by default in literals to be more convenient. Certainly it’s more common if you survey popular languages — Matlab is very unusual here in requiring you to call a function to access character escapes.

2 Likes

#16

Thanks to everyone for taking time to explain it. Also I see your point, @stevengj, that Matlab might have given me a wrong perception of what is good / bad, since it chooses to act differently to other languages.

@kristoffer.carlsson your example was much appreciated, I finally got why it isn’t possible as such.

I think I got my answer :slight_smile:

Kind regards

0 Likes