0

I have to get the path of a directory from Java Text field and store it on another string variable but it needs the code of java to add/insert \ after the colon : character.

String folderFath="D:\TF";

I need to add \ after the : character. I need to get string variable D:\\TF

Vipin Yadav
  • 1,616
  • 1
  • 14
  • 23

2 Answers2

0

have you tried this way "D:\\TF"?

yuzuriha
  • 465
  • 1
  • 8
  • 18
0

I believe this answer has exactly what you're looking for. Note that I would have preferred a tag for the language you're using to be sure.

String.replaceAll single backslashes with double backslashes

This code does what I expected it to:

  public class JavaFiddle
  {
    public static void main(String[] args)
    {
        String myString = "C:\\Text\\Somewhere\\Works"; 
        System.out.println(myString);
        String myStringTwo = myString.replace("\\", "\\\\");
        System.out.println(myStringTwo);
    }
  }

Note: What's going on here is the first backslash is an 'Escape Character.' You can't add a backslash directly into a string because it indicates programmatic control. https://en.wikipedia.org/wiki/Escape_character

Dylan Brams
  • 2,089
  • 1
  • 19
  • 36