Android工具类——字符串相关(判断特殊字符/验证中文名字)

    /**
     * 判断是否包含特殊字符
     * @return  false:未包含 true:包含
     */
    public static boolean inputJudge(String editText) {
        String speChat = "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]";
        Pattern pattern = Pattern.compile(speChat);
        Log.d("inputJudge", "pattern: "+ pattern);
        Matcher matcher = pattern.matcher(editText);
        Log.d("inputJudge", "matcher: "+ matcher);
        if (matcher.find()) {
            return true;
        } else {
            return false;
        }
    }
    /**
     * 验证输入的名字是否为“中文”或者是否包含“·”
     * @param str w为用户输入的姓名
     * @return
     */
    public static boolean verifyName(String str) {
        if (str.contains("·") || str.contains("•")) {
            if (str.matches("^[\\u4e00-\\u9fa5]+[·•][\\u4e00-\\u9fa5]+$")) {
                return true;
            } else {
                return false;
            }
        } else {
            if (str.matches("^[\\u4e00-\\u9fa5]+$")) {
                return true;
            } else {
                return false;
            }
        }
    }

猜你喜欢

转载自blog.csdn.net/qq_30297763/article/details/97235263