As we all know,we can use
string aa=@"E:\dev_workspace1\AccessCore\WebRoot\DataFile"
in c# in order not to double the '\'.
But how to do in java?
So in a character literal, you need to escape single quotes, e.g. '\'' . So all you need is "\\'" , escaping only the backslash.
Use the backslash character to escape a single character or symbol. Only the character immediately following the backslash is escaped.
These are escape characters which are used to manipulate string. \t Insert a tab in the text at this point. \b Insert a backspace in the text at this point. \n Insert a newline in the text at this point.
However, we know that the backslash character is an escape character in Java String literals as well. Therefore, we need to double the backslash character when using it to precede any character (including the \ character itself).
Unfortunately, there is no full-string escape operator in Java. You need to write the code as:
String aa = "E:\\dev_workspace1\\AccessCore\\WebRoot\\DataFile";
There is no whole string escape operator but, if it's for file access, you can use a forward slash:
String aa="E:/dev_workspace1/AccessCore/WebRoot/DataFile";
Windows allows both forward and backward slashes as a path separator. It won't work if you pass the path to an external program that mangles with it and fails, but that's pretty rare.
Might not be a direct answer to your question, but I feel this should be pointed out:
There's a system-dependent default name-separator character.
The really system-independent way is to do this:
String aa = "E:/dev_workspace1/AccessCore/WebRoot/DataFile";
String output = aa.replace('/', File.separatorChar);
It will give you "E:\dev_workspace1\AccessCore\WebRoot\DataFile" on Windows and "E:/dev_workspace1/AccessCore/WebRoot/DataFile" just about everywhere else.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With