java中的可打印字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/220547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
printable char in java
提问by OscarRyz
Does anyone knows how to detect printable characters in java?
有谁知道如何在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.
我通过 KeyListener 获取输入,然后 Ctr-'key' 打印出一个正方形。有了这个功能似乎就够了。
Am I missing some char here?
我在这里错过了一些字符吗?
采纳答案by OscarRyz
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;
}
回答by jb.
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:
我不确定我是否理解你的问题。但是,如果您想检测字符是否可以绘制到 Graphics 对象,并且如果不打印一些占位符字符,您可能会发现有用:
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.
它将检查字体是否可以显示特定的代码点(更多的是检查字体是否可显示 - 因为有可显示的字符 - 比如? - 但有些字体无法显示它们。