Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

small doubt in escape sequences

 
Johnson David
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My doubt is regarding backspace , /b

If i use /b in a string literal like the fallowing

String bs="this is my back/bspace";

if i print the bs i will get -- this is my bacspace

then i will use it at the end of the string literal like below

String bs="this is my backspace/b";

if i prints bs, the output is --this is my backspace

My doubt is ,why /b has no effect when used at the end of a string literal?



thanx

johnson
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"johnson" please read our JavaRanch Naming Policy. We encourage you to use your real name in the format <first name> <family name>. You can change your displayed name via the My Profile link.
Thanks,
-Barry


(NR)
[ May 25, 2005: Message edited by: Barry Gaunt ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An escape sequence uses the back slash "\" not the forward facing slash "/".
So use "\b", not "/b".
The behaviour will also depend on how the output device handles the backspace character.
 
Johnson David
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Barry Gaunt, i havent read about ur naming convesion ,ok now i changed my profile , also my question is wrong i have used backspace symbol as /b instead of \b





thanx

johnson
 
fred rosenberger
lowercase baba
Bartender
Posts: 12085
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
backspace is not a delete. so when you have "this is my back/bspace", you start printing the characters in order...

b...a...c..k... then you back up one spot, and print b. this overwrites the k with a b.

in your second case, you print all the letters through the e, then back up one... but never overwrite the e with anything else, so it stays where it is.
 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but never overwrite the e with anything else, so it stays where it is.[/QB]


why it doesn't overwrite e ?
 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
??? no reply ?
 
Alan Jump
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by amit taneja:


why it doesn't overwrite e ?


Because the 'e' is at the end of the output string; there's nothing after the \b to overwrite it with. So the 'e' gets sent to the output device, then the output device is told to back up one space...and that's it. If the intent was to have the 'e' overwritten/removed, the string should have been something like Note the trailing space; that will overwrite the 'e'.

Hope this helps...
 
fred rosenberger
lowercase baba
Bartender
Posts: 12085
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
think of a backspace as hitting the left arrow key on your keyboard (and you have the "insert" turned off). if i type the letters

"abcde"

and then hit the left arrow, the cursor is between the d and e. if that's ALL i do, the e does not get overwritten. you haven't given it anything to overwrite the 'e' WITH. it's sitting there, saying "ok, i backed up to re-write whatevers in that spot, just tell me what it is..." but then you say "we're done writing - you can quit now", so the 'e' never gets written over, so it stays there.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic