How to display \ and " in Java?

vm _1_r :

How do you display the characters \ and "?

One backslash (\) +and + " (That's the needed output)

I got smth near but without " . The answer on a website shows inoperative answer...

System.out.println('\' and '\"');
Caius Jard :

I'm presuming this is Java

//this code
System.out.println("Backslash is \\ and quote is \". The end.");

//will print this string into the console:
Backslash is \ and quote is ". The end.

Because \ is used to turn normal characters like n or t into special characters like NEWLINE \n or TAB \t. If you want to print out a backslash, you can't just write a single backslash on its own in a string because Java will then look at the next character after the backslash to know what to do. Because java needs another character after a backslash, it's a rule of the language that if you want to actually print a backslash you have to put a second backslash after the first. If you wanted to print two backslashes in succession, you would have to write FOUR backslashes in succession into your code.

//this code
System.out.println("Double Backslash is \\\\. The end.");

//will print this string into the console:
Double Backslash is \\. The end.

Because " is used to start and stop strings in code, you also need a way to indicate to Java that "i'm going to write a quote but it's to be printed literally, it doesn't stop the string", and for that you precede the " with a backslash like \"

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=3664&siteId=1