File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Character.getType() ?

 
bob connolly
Ranch Hand
Posts: 204
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, when i use the Character.getType(_) method, it returns an int value, i'm wondering how to convert that int value, ie the 15 below, to a meaningfull literal like "CONTROL" or "isaCONTROL" character or something like that!

Thanks very much!



t 9 15 <<<<<
n 10 15
r 13 15
f 12 15
 
Philip Heller
author
Ranch Hand
Posts: 119
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great question, Bob!

There's no good way to do it. As you've seen from the API page, Character has a bunch of static constants. How do you translate a number to a name? I guess you could do this:



But it seems wrong. Below is a way to do it using reflection. The code scans all field names in java.lang.Character, looking for the first byte that matches the getType() of the char. As a general approach that's 99% good enough in my experience, but your actual mileage may vary. It's still cheesy.

I can't think of a better approach, and the moral is: use enums where possible. If the char types were enums rather than bytes, toString() would automatically return the enum's name, which would be exactly what you want.

Here's the reflection code.

--Phil

 
bob connolly
Ranch Hand
Posts: 204
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Much Thanks Philip!

Have lots to study here, really appreicate the suggestions!

Have a great week!

bc
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic