• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

query with escape characters

 
Moieen Khatri
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Thanks in advance
 
Moieen Khatri
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is the source from K & B book :

 
Kelvin Chenhao Lim
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
Moieen Khatri
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the example...it really helped!!

Cheers
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic