• Post Reply Bookmark Topic Watch Topic
  • New Topic

Unicode Numeric Value...  RSS feed

 
Landon Blake
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to test the value of a Character object to see if it is a "newline".

The Javadoc for the getNumericValue of the Character class says that the method "Returns the Unicode numeric value of the character as a nonnegative integer."

Where do I find the "numeric value" of the newline character?

I checked the Unicode site here: http://www.unicode.org/charts/PDF/U0000.pdf

It told me the code for the newline character is "000A". I think this is a hexidecimal value. Do I need to convert it to a decimal value to do the comparison? Is there a better way to run this test? Should I be using a different method if the Character class?

Thanks for the help,

Landon
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably the most readable way to refer to a newline is as '\n'. That's a character literal; though it looks like two characters, it represents one character, the newline. You can also use this as part of a string literal. So you can write code like

or

You could also use the numberic value to represent the character. E.g. the first line of code above could also be written

(using hexadecimal notation), or

(in decimal). Most people haven't got these memorized though, so in Java you're usually better off using \n.

Also, be aware that the term "newline" is somewhat ambiguous depending who you talk to. It may represent a single character, ASCII value 10, which is what peope usually mean in Java, and how I interpreted your question. Or it may refer to the more general concept of a line separator, which can be more than one character, depending on your system, and what you're doing. Generally a line separator is a '\n' character on Unix-based systems, and it's a '\r' followed by '\n' (two characters total) on Windows. If you're certain that what you're talking about is really just a single '\n', then great, ignore this paragraph. If you're not certain though, you may want to describe a little more about what you're trying to do here, so we can offer better info on how you can handle it.
 
Landon Blake
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the response Jim.

I'm hoping to recognize the newline characters as I parse a string so that I can increment a variable that counts the "lines" in string. I think I can use the character literal '/n' that you have suggested.

Landon
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!