Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

printable char in java

Does anyone knows how to detect printable characters in java?

After a while ( trial/error ) I get to this method:

    public boolean isPrintableChar( char c ) {         Character.UnicodeBlock block = Character.UnicodeBlock.of( c );         return (!Character.isISOControl(c)) &&                 c != KeyEvent.CHAR_UNDEFINED &&                 block != null &&                 block != Character.UnicodeBlock.SPECIALS;     } 

I'm getting the input via KeyListener and come Ctr-'key' printed an square. With this function seems fairly enough.

Am I missing some char here?

like image 303
OscarRyz Avatar asked Oct 21 '08 02:10

OscarRyz


People also ask

Can you print a char in Java?

The print(char) method of PrintStream Class in Java is used to print the specified char value on the stream. This char value is taken as a parameter. Parameters: This method accepts a mandatory parameter charValue which is the char value to be written on the stream. Return Value: This method do not returns any value.

What characters are printable?

Printable characters include letters, digits, and special punctuation such as commas, brackets, and question marks. Unprintable characters correspond to codes that indicate a special function such as a line feed, tab, or carriage return.

What is \\ x00 -\\ x7F?

US-ASCII is a character set (and an encoding) with some notable features: Values are between 0–127 (x00–x7F) ASCII code-point 32 (decimal) represents a SPACE. ASCII code-point 65 represents the uppercase letter A.

How do I know if a character is printable?

C isprint()The isprint() function checks whether a character is a printable character or not. Those characters that occupies printing space are known as printable characters. Printable characters are just the opposite of control characters which can be checked using iscntrl().


2 Answers

It seems this was the "Font" independent way.

public boolean isPrintableChar( char c ) {     Character.UnicodeBlock block = Character.UnicodeBlock.of( c );     return (!Character.isISOControl(c)) &&             c != KeyEvent.CHAR_UNDEFINED &&             block != null &&             block != Character.UnicodeBlock.SPECIALS; } 
like image 186
OscarRyz Avatar answered Oct 02 '22 02:10

OscarRyz


I'm not perfectly sure whether I understand your problem. But if you want detect if character can be drawn to Graphics object, and if not print some placeholder char you might find usefull:

Font.canDisplay(int) 

It will check whether font can display specific codepoint (it is more that check whether font is displayable at all -- since there are chars that are displayable - like ą - but some fonts cant display them.

like image 22
jb. Avatar answered Oct 02 '22 03:10

jb.