Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if there is/are escape character(s) in string

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)?

like image 856
DnR Avatar asked Jan 22 '26 14:01

DnR


1 Answers

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.

like image 96
Sotirios Delimanolis Avatar answered Jan 25 '26 03:01

Sotirios Delimanolis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!