邮箱去除特殊符号

橙三吉。 提交于 2019-11-29 21:31:58
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;
    }

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!