java 过滤特殊字符和表情

public class EmojiFilter {
    /**
     * 检测是否有非法字符
     * @param source
     * @return 一旦含有就返回true
     */
    public static boolean containsEmoji(String source) {    
        int len = source.length();
        for (int i = 0; i < len; i++) {
        	//使用charcode而不是直接用char是因为有些字符需要两个char来表示 lowsurragate  highsurrgate
            int codePoint = Character.codePointAt(source, i);
            return isEmojiCharacter(codePoint);
        }
        return false;
    }
    /**
     * 判断emj表情
     * @param codePoint
     * @return 
     */
    public static boolean isEmojiCharacter(int codePoint) {
        return (0x0080<=codePoint&&codePoint<=0x02AF)||
                (0x0300<=codePoint&&codePoint<=0x03FF)||
                (0x0600<=codePoint&&codePoint<=0x06FF)||
                (0x0C00<=codePoint&&codePoint<=0x0C7F)||
                (0x1DC0<=codePoint&&codePoint<=0x1DFF)||
                (0x1E00<=codePoint&&codePoint<=0x1EFF)||
                (0x2000<=codePoint&&codePoint<=0x209F)||
                (0x20D0<=codePoint&&codePoint<=0x214F)||
                (0x2190<=codePoint&&codePoint<=0x23FF)||
                (0x2460<=codePoint&&codePoint<=0x25FF)||
                (0x2600<=codePoint&&codePoint<=0x27EF)||
                (0x2900<=codePoint&&codePoint<=0x29FF)||
                (0x2B00<=codePoint&&codePoint<=0x2BFF)||
                (0x2C60<=codePoint&&codePoint<=0x2C7F)||
                (0x2E00<=codePoint&&codePoint<=0x2E7F)||
                (0xA490<=codePoint&&codePoint<=0xA4CF)||
                (0xE000<=codePoint&&codePoint<=0xF8FF)||
                (0xFE00<=codePoint&&codePoint<=0xFE0F)||
                (0xFE30<=codePoint&&codePoint<=0xFE4F)||
                (0x1F000<=codePoint&&codePoint<=0x1F02F)||
                (0x1F0A0<=codePoint&&codePoint<=0x1F0FF)||
                (0x1F100<=codePoint&&codePoint<=0x1F64F)||
                (0x1F680<=codePoint&&codePoint<=0x1F6FF)||
                (0x1F910<=codePoint&&codePoint<=0x1F96B)||
                (0x1F980<=codePoint&&codePoint<=0x1F9E0);


    }
    /**
     * 判断是否包含特殊字符
     * @param str
     * @return 包含返回true
     */
    public static boolean isContainSpecial(String str){
    	String regEx="[`~!@#$%^&*()+=|{}':;'\\[\\]<>/?~!@#¥%……&*——+|{}【】‘;:”“’。,、?().,-]";
    	Pattern p=Pattern.compile(regEx);
    	Matcher m=p.matcher(str);    	
    	return m.find();
    }
}

 

你可能感兴趣的:(java)