8

For my command-line-interfaces it's often nice to have a little ASCII art in the beginning, but those often contain many backslashes.

For example:

System.out.println("  _____ _______       _____ _  __ ");
System.out.println(" / ____|__   __|/\   / ____| |/ / ");
System.out.println("| (___    | |  /  \ | |    | ' /  ");
System.out.println(" \___ \   | | / /\ \| |    |  <   ");
System.out.println(" ____) |  | |/ ____ \ |____| . \  ");
System.out.println("|_____/   |_/_/    \_\_____|_|\_\ ");

But since every \ needs to be a \\ this often looks very ugly in code and it's very hard to find/fix an error in the 'font'. Is there a way to tell Java NOT to use escape sequences?

Community
  • 1
  • 1
trichner
  • 840
  • 1
  • 11
  • 22
  • 2
    "\\" is the only way to go. If you *really* don't want the double- backquotes, you can always 1) create a static String[] array with each line of text, 2) substitute some "acceptable" marker character for your slash, 3) println() the array in a loop, and 4) String.replace() the marker character with "\\". – paulsm4 Apr 07 '12 at 16:27

5 Answers5

3

No, Java doesn't have anything like the "verbatim string literals" of C#. If you want to do ASCII art, consider putting it into a text file instead and loading that from the code.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

read that "image" from file, that file can be in your jar library :)

viliam
  • 503
  • 6
  • 23
2

Hate to gravedig this thread, but for anyone else out there looking for this.. Try using unicode!

For example: \u0092 = "\"

Joe
  • 29
  • 1
1

Sorry, dude. No such animal exists.

phatfingers
  • 9,770
  • 3
  • 30
  • 44
0

Perhaps a little late but this is for anyone still looking for a solution;

Pattern.quote() can be used to ignore escape sequences.

System.out.println(Pattern.quote("|     |   ____    |       |        ______  "))
System.out.println(Pattern.quote("|     |  |        |       |       /      \ "))
System.out.println(Pattern.quote("|_____|  |__      |       |       |      | "))
System.out.println(Pattern.quote("|     |  |        |       |       |      | "))
System.out.println(Pattern.quote("|     |  |_____   |_____  |_____  \______/ "))
//I apologize for the rushed textblock

More examples

One thing to note escape sequences \Q and \E will be at the start and end of your string

username 66
  • 5
  • 1
  • 2
  • Your code doesn't compile for me (JDK 17 on Windows 11). The backslash (i.e. `\`) still has to be escaped (i.e. `\\`). – Abra Aug 31 '23 at 05:23