• Post Reply Bookmark Topic Watch Topic
  • New Topic

Escape backslash for char  RSS feed

 
Donna Jane
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Below is from the Bates and Sierra book, chapter 3:

You can also use an escape code if you want to represent a character that can't be
typed in as a literal, including the characters for linefeed, newline, horizontal tab,
backspace, and single quotes.





But for the newline code that is escaped above, it still gives me a new line. Is this a typo? Shouldn't it be '\\n' for it to be escaped?



 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Donna,
Not a typo.


This outputs a new line.


This outputs the string \n. The reason is that \\ is escaping the slash itself. So you get the backslash string followed by a letter that happens to be "n"


Similarly, this outputs the string \a


It gets interesting here. This one doesn't compile because \a is not a valid escape sequence. The compiler is smart enough to look at what is going on!

And welcome to CodeRanch!
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not to confuse matters, but this will be a little different when you get to the part about regular expressions. They you will write Strings like "\\w+". The reason there is that Java turns that into "\w+". But the regular expression itself requires a backslash. So you have two.

(Don't worry about this part yet. Just file away that you read it if you are confused when you get that far)
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again, welcome to the Ranch
 
Donna Jane
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I understand what the \n is supposed to do. However, the text says "You can also use an escape code if you want to represent a character that can't be
typed in as a literal, including the characters for linefeed, newline, horizontal tab,backspace, and single quotes". So, to me that means that if you want that character to print you would do



But it does not do that. It creates a new line. If you want the \n to print, you have to do '\\n'.

Am I misinterpreting the explanatory text? Are they just trying to say what the \n is for?

Just trying to understand what they are saying.

Thanks for the welcome Campbell! Good to be here.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Donna Jane wrote: However, the text says "You can also use an escape code if you want to represent a character that can't be
typed in as a literal, including the characters for linefeed, newline, horizontal tab,backspace, and single quotes". So, to me that means that if you want that character to print you would do



So far, your understanding is correct. But it does do that. The new line that gets created IS what \n is. That's why it can't be typed in as a literal. How would you type in a line break? You can't hit "return" or "enter" because that would actually cause a new line. And in Java, lines have meaning.

When you print out \n using \\n, you aren't printing a new line. You are printing the representation of a new line.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Donna Jane wrote:But it does not do that. It creates a new line. If you want the \n to print, you have to do '\\n'.

Which, I think you'll find, is not a valid character literal.

Personally, I dislike the fact that Java chose to use the same escape character for Strings as regexes do; but there's not much I can do about it, and I suspect they chose it because it's familiar to most people.

One alternative, when writing regexes, which works for most things, is to use square brackets, eg:
myString.split("[*]");
rather than escapes:
myString.split("\\*");

The other is to use the Unicode value, eg: '\u000a'.

HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this is getting confusing. You can't use it \u000a String literals. Can you actually write \u000a in a String literal for a regex, then?
Even more confusingly, you should avoid \n unless somebody told you they want an LF character for reasons given here.
 
Donna Jane
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regex is confusing. But I do understand now what they are trying to say in chapter 3 of the aforementioned text. Thank you, Jeanne, for your explanation, and Winston and Campbell for your responses. Love this board and I'm sure I'll be back!

Donna
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done working it out
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Can you actually write \u000a in a String literal for a regex, then?

I don't see why not; and you don't have to "double up" on the escapes. However, you then have a regex that includes a newline, which I would assume would be dealt with just like any other character - but TBH, I'm not sure, because I've never used "multi-line" regexes in anger.

I presume that Java regexes can handle it, because I can't think of any other way of including, for example, a Chinese character in one.

Winston
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I always thought the Unicode escapes were translated by a preprocessor before compilation, so the compiler would see that as a String on two lines, and throw an unclosed String error. But I may be mistaken.
i tried Strings with \uabcd or similar in from the command line yesterday, and they came out as "\uabcd"!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:i tried Strings with \uabcd or similar in from the command line yesterday, and they came out as "\uabcd"!

Seems reasonable to me, since most "cmd.exe" screens (or Unix terminals, for that matter) aren't set up for displaying Unicode characters. If I put a String like that in a GUI text field, however, I would expect to see "the character".

But that's just my interpretation.

Winston
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will have to try it out
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!