Avoiding "\" characters in Java

As we all know, we can use

string aa=@"E:\dev_workspace1\AccessCore\WebRoot\DataFile" 

in C # so as not to double the value of '\'.

But how to do it in java?

+5
source share
5 answers

Unfortunately, in Java there is no full-screen escape statement. You need to write code like:

String aa = "E:\\dev_workspace1\\AccessCore\\WebRoot\\DataFile";
+20
source

There is not a whole line escape statement, but if it is to access a file, you can use a slash:

String aa="E:/dev_workspace1/AccessCore/WebRoot/DataFile";

Windows allows both forward and backslashes as path separators. This will not work if you go the path to an external program that will cope with it and work, but this is quite rare.

+12
source

, , :

- .

+4

- :

String aa = "E:/dev_workspace1/AccessCore/WebRoot/DataFile";
String output = aa.replace('/', File.separatorChar);

   "E:\dev_workspace1\AccessCore\WebRoot\DataFile" Windows    "E:/dev_workspace1/AccessCore/WebRoot/DataFile" .

+3

, '/' Java. "/" Java (\ under windows,/under unix). , , "\" . "E:/dev_workspace1/AccessCore/WebRoot/DataFile".

'\' Java-String, : " a".

+1

All Articles