3

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
  • 72,509
  • 5
  • 49
  • 80
Vmir
  • 79
  • 11
  • 2
    Please consider adding language tags to the question and formatting the code in a code block section. – mrVoid Feb 25 '20 at 10:50
  • 1
    @webprogrammer Adding code formats is one thing, but you also edited the code to remove one of the errors the OP made. Don't do that. If you're going to format code, just format it - don't start fixing it too. Fix it in an answer – Caius Jard Feb 25 '20 at 12:18
  • @CaiusJard, I have added only triple backwards single quotes before code and after it. I didn't remove anything. Most probably it was done by the script on saving. – webprogrammer Feb 25 '20 at 13:32

2 Answers2

4

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 \"

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
  • Well thanks, but I need to print " \ and " ". One backslash (\\) +and + " – Vmir Feb 25 '20 at 11:07
  • Pretty sure you can copy the code out of the post, delete words, like `Backslash`, `is`, `quote`, `is`, `The` and `end` (plus all the associated spaces and punctuation) and get to where you want to go? – Caius Jard Feb 25 '20 at 12:16
  • Thanks, it was throwing exceptions but now it does not. Thank you. – Vmir Feb 26 '20 at 02:48
0

@Caius Jard gace you the way to do it, just escape the character.

System.out.println("\\" + and + "\""); // and is a variable