printable char in java

僤鯓⒐⒋嵵緔 提交于 2019-11-26 08:15:56

问题


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?


回答1:


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;
}



回答2:


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.



来源:https://stackoverflow.com/questions/220547/printable-char-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!