Attempts to determine if a character is a legal alphabetic, digit, or special character. - Java java.lang

Java examples for java.lang:char

Description

Attempts to determine if a character is a legal alphabetic, digit, or special character.

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        char c = 'a';
        System.out.println(isLegitimateCharacter(c));
    }/* w ww .j av a  2  s  . c  o  m*/

    /**
     * Attempts to determine if a character is a legal alphabetic, digit, or special character. 
     * A special character is defined as any of the following:<i> /*!@#$%^&*()\"{}_[]|\\?/<>,. </i> (includes ' ')
     * @return a boolean which should indicate if the provided char is an alphabetic, digit, or special character 
     */
    public static boolean isLegitimateCharacter(char c) {
        return Character.isLetterOrDigit(c) || isSpecialCharacter(c);
    }

    /**
     * Determines if the provided character is a special character, that is to say it belongs to the following set:
     * <i>/*!@#$%^&*()\"{}_[]|\\?/<>,. </i> (includes ' ')
     * @param c a char to check against the set of special characters
     * @return a boolean, true if the provided char falls into the set of special characters, otherwise false
     */
    public static boolean isSpecialCharacter(char c) {
        String specialChars = "/*!@#$%^&*()\"{}_[]|\\?/<>,. ";
        for (int i = 0; i < specialChars.length(); i++) {
            if (c == specialChars.charAt(i)) {
                return true;
            }
        }
        return false;
    }
}

Related Tutorials