Check out Manning's Countdown to 2014. Use discount code crdotd14 all month for 50% off every deal.
Big Moose Saloon
 Search | Java FAQ | Recent Topics Register / Login

query with escape characters

Moieen Khatri
Ranch Hand

Joined: Nov 27, 2007
Posts: 144
Hi,

Can some one please explain why a "\\" is used in a String to represent a "\"

For e.g

E:\A>java SplitTest "121313 aK1213131WRWKRW" "\D"

now how can I use the "\D" command line argument as a String and what's the advantage of the same.

Moieen Khatri
Ranch Hand

Joined: Nov 27, 2007
Posts: 144
here is the source from K & B book :

Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Originally posted by Moieen Khatri:
Can some one please explain why a "\\" is used in a String to represent a "\"

That's because '\' is the escape character for character and string literals. It's used as a prefix for special characters, such as \n (newline), \t (tab), and \' (single quote). Most of these characters cannot appear directly within a literal.

Now, imagine that you want to print the sequence c:\too\noob. To do this, you can't just write "c:\too\noob" as a string literal in your Java program, since this will be interpreted as "c:" + (tab) + "oo" + (newline) + "oob". To get around this, you would use the sequence \\ to denote that you really do want a backslash character. Thus you'd write the string literal as "c:\\too\\noob".

SCJP 5.0
Moieen Khatri
Ranch Hand

Joined: Nov 27, 2007
Posts: 144
thanks for the example...it really helped!!

Cheers

It is sorta covered in the JavaRanch Style Guide.

subject: query with escape characters