Question

Quelqu'un sait-il comment détecter les caractères imprimables en Java?

Après un certain temps (essai / erreur), j'arrive à cette méthode:

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

Je reçois l'entrée via KeyListener et viens Ctr-'key 'imprimé un carré. Avec cette fonction semble assez juste.

Me manque-t-il un personnage ici?

Était-ce utile?

La solution

Il semble que ce soit la "Police". manière indépendante.

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

Autres conseils

Je ne suis pas tout à fait sûr de bien comprendre votre problème. Mais si vous voulez détecter si un caractère peut être dessiné dans l’objet Graphics, et si vous n’imprimez pas un caractère fictif, vous pourriez trouver utile:

Font.canDisplay(int)

Il vérifiera si une police peut afficher un point de code spécifique (c’est plus que vérifier si la police est affichable ou non - car il y a des caractères qui peuvent être affichés - comme & # 261; - mais certaines polices ne peuvent pas les afficher.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top