A way to consider the backslash as a normal character

Is there someone who knows how to consider the backslash as a normal character (not an escape character) in a string in Java?

Any help will be appreciated!

Jon Skeet
people
quotationmark

No, Java doesn't have anything like the verbatim string literals of C# and other languages.

Backslash is always an escape character in a Java string or character literal. Note that it's only in literals that Java cares, as a language. The language itself has no special behaviour when it comes to backslashes which already exist within string objects. Some libraries (e.g. regular expressions) treat backslash specially too, but that's a very different matter... and it's important to differentiate between "I already have a backslash in my string, and I'm trying to use the string in a particular context which is sensitive to backslashes" and "I'm trying to create a string with a backslash in, within Java source code, using a string literal".

If you have a text which can include backslashes and you want to simplify it for readability, two options present themselves:

  • Use a different character (e.g. forward slash) and then use String.replace to replace all occurrences of the other character with backslash
  • Put your text in a text file which you load at execution time

people

See more on this question at Stackoverflow