邮箱去除特殊符号

public static String mailFormatInfo(String mail){
        try {
            if(StringUtil.isEmpty(mail)){
                return mail;
            }

            //1,去除空格,特殊字符
            mail = mail.trim();
            mail = removeCR(mail);
            mail = removeSpace(mail);
            mail = mail.replace("\u00A0", "");

            //2,循环去符号
            if(mail.contains(MAIL_SPIT)){
                //替换@符号之前的内容
                String preFix = mail.substring(0, mail.indexOf(MAIL_SPIT));
                if(!org.springframework.util.StringUtils.isEmpty(preFix)){
                    int preLength = 0;
                    int sub = 0;
                    do{
                        //替换之前的长度
                        preLength = preFix.length();
                        //替换符号
                        String first = preFix.substring(0,1);
                        first = first.replaceAll("[\\pP\\p{Punct}]","");
                        preFix = first + preFix.substring(1);
                        //替换长度
                        sub = preLength - preFix.length();
                    }while (sub > 0 && preFix.length() > 0);
                }
                //替换@符号之后的内容
                String lastFix = mail.substring(mail.indexOf(MAIL_SPIT) + 1);
                if(!org.springframework.util.StringUtils.isEmpty(lastFix)){
                    int preLength = 0;
                    int sub = 0;
                    do{
                        //替换之前长度
                        preLength = lastFix.length();
                        //替换符号
                        String last = lastFix.substring(lastFix.length() - 1, lastFix.length());
                        last = last.replaceAll("[\\pP\\p{Punct}]","");
                        lastFix = lastFix.substring(0, lastFix.length() -1) + last;
                        //替换长度
                        sub = preLength - lastFix.length();
                    }while (sub > 0 && lastFix.length() > 0);
                }
                //组装最后替换结果
                mail = preFix + MAIL_SPIT + lastFix;
            }

            //3,去除空格,特殊字符
            mail = mail.trim();
            mail = removeCR(mail);
            mail = removeSpace(mail);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mail;
    }

 

转载于:https://www.cnblogs.com/wanhua-wu/p/11533707.html

你可能感兴趣的:(邮箱去除特殊符号)