-2

I want to store \ in a string variable #

String var= "\" ;

public class Main
{
    public static void main(String[] args) {
        String var="\"
        System.out.println(var);
    }
}

2 Answers2

1
String var="\\";

will give you the backslash you want

Jeremy Kahan
  • 3,796
  • 1
  • 10
  • 23
0

From Characters

A character preceded by a backslash (\) is an escape sequence and has special meaning to the compiler. The following table shows the Java escape sequences:

\\ Insert a backslash character in the text at this point.

enter image description here

So in you case use below code:

String var = "\\";
Community
  • 1
  • 1
Sumit Singh
  • 15,743
  • 6
  • 59
  • 89