3

Let say I have

String str="hello\" world\\";

when printing str, the output is

hello" world\

even when printing str.length() the output is

13

Is there any way to prove that str value has escape character(s)?

DnR
  • 3,487
  • 3
  • 23
  • 31
  • Why are you trying to do this? Just curiosity – Christian Tapia Feb 28 '14 at 05:46
  • 1
    @KugathasanAbimaran - Special character != Escape Character. – Rahul Feb 28 '14 at 05:48
  • 1
    Not really sure if its possible, but [this seems to be a good read](http://stackoverflow.com/q/12260872/2024761). – Rahul Feb 28 '14 at 05:50
  • @Christian I don't know how to explain _'why'_, but just curious if it is possible – DnR Feb 28 '14 at 05:51
  • 1
    Can check link - http://stackoverflow.com/questions/1327355/is-there-a-java-function-which-parses-escaped-characters – Aryan Feb 28 '14 at 05:54
  • I am thinking that whenever we print a `string` and the output contains character such as `"` and \, then we can conclude that those character, `"` and \ was escaped? – DnR Feb 28 '14 at 06:00

2 Answers2

3

There is no such thing as escape characters at run time.

Escape characters appear only in String literals. For example,

String literal = "Some\nEscape\rSequence\\\"";

At compilation time, the compiler produces a String value with their actual binary representation (UTF-8 iirc). The JVM uses that String value directly.

You wrote

I am thinking that whenever we print a string and the output contains character such as " and \, then we can conclude that those character, " and \ was escaped?

This is not true, those characters might have been read from a file or some other InputStream. They were definitely not escaped in a text file.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
0

Yes.

Use the Apache Commons Library, specifically StringEscapeUtils#escapeJava.

jshell> StringEscapeUtils.escapeJava("Newline \n here \u0344 and unicode \f\n\r\t\"\0\13 and more")
$136 ==> "Newline \\n here \\u0344 and unicode \\f\\n\\r\\t\\\"\\u0000\\u000B and more"

This prepends a backslash to each escape sequence and also swaps the variable-width octal sequences for fixed-width Unicode sequences. This means that every escape sequence will consist of "\\" two backslashes, followed by one of {n, b, r, t, f, ", \}, or a 'u' character, plus exactly four hexadecimal [0-F] digits.

If you just want to know whether or not the original String contains escape sequences, search for "\\" in the Apache-fied string. If you want to find the positions of those sequences, it's a bit more involved.

See more at this Gist.

awwsmm
  • 1,353
  • 1
  • 18
  • 28