• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

query with escape characters

 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here is the source from K & B book :

 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the example...it really helped!!

Cheers
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic