1

I have a plain String where it contains "\". However Java seems to be trying to format the String when it sees the Double Backslash. Is there a way to tell the String to completely ignore Backslash formatting?

String Test = "File location is: C:\\Folder\\File.txt";

Output:

Test = "File location is: C:\Folder\File.txt"

Desired output:

Test = "File location is: C:\\Folder\\File.txt"
khelwood
  • 55,782
  • 14
  • 81
  • 108
Adam Deen
  • 35
  • 7
  • 4
    If you want a backslash in a string, you need to put ```\\``` in the string literal. If you want ```\\``` in a string, you need to put ```\\\\``` in the string literal. – khelwood Sep 17 '20 at 13:37
  • You could try this: [https://stackoverflow.com/questions/23363241/escaping-backslash-in-java-string-literal](https://stackoverflow.com/questions/23363241/escaping-backslash-in-java-string-literal) – Priyatham sai chand Sep 17 '20 at 13:38
  • @khelwood, thanks a lot!. Could you repost your comment as an answer so I can cross it as solution :D – Adam Deen Sep 17 '20 at 13:39

1 Answers1

0

Try this :

public class Sample {

     public static void main(String[] args){
        String test = "File location is: C:\\\\Folder\\\\File.txt";
        System.out.println("Test = "+ test);
     }
}

Output :

Test = File location is: C:\\Folder\\File.txt
Anish B.
  • 9,111
  • 3
  • 21
  • 41