替换特殊符号

时间:2022-11-02 20:44:05
public class BaseReplaceString {

  /*  <  < 小于号或显示标记
    >  > 大于号或显示标记
    &  & 可用于显示其它特殊字符
    " "  引号
    ® ®  已注册
    © © 版权
    ™ ™ 商标
        半个空白位
        一个空白位
      不断行的空白*/
    private static String[] htmlSpecialCharacter = new String[]{"<", ">", "&", "®", "©", "™", " ", " ", " "};
    private static String[] commonSpecialCharacter = new String[]{"<", ">", "&", "\"", "'", " ", "!", "@", "#", "\\$", "%", "\\*", "\\^", "\\(", "\\)", "_", "\\+", "\\=", "\\-", ",", "\\.", ",", "。", "\\?", "`", "~","《","》",".com",".cn",".net","org","www"};


    /**
     * 过滤html特殊字符
     * @param specialCharacterStr
     * @return
     */
    public static String htmlSpecialCharacterRemover(String specialCharacterStr) {
        int length = htmlSpecialCharacter.length;
        for (int i = 0; i < length; i++) {
            specialCharacterStr = specialCharacterStr.replace(htmlSpecialCharacter[i], "");
        }
        return specialCharacterStr;
    }


    /**
     * 过滤普通的特殊字符
     * @param specialCharacterStr
     * @return
     */
    public static String commonsSpecialCharacterRemover(String specialCharacterStr) {
        int length = commonSpecialCharacter.length;
        for (int i = 0; i < length; i++) {
            specialCharacterStr = specialCharacterStr.replace(commonSpecialCharacter[i], "");
        }
        return specialCharacterStr;
    }


    /**
     * 所有特殊字符替换
     * @param specialCharacterStr
     * @return
     */
    public static String specialCharacterRemover(String specialCharacterStr) {
        specialCharacterStr = htmlSpecialCharacterRemover(specialCharacterStr);
        specialCharacterStr = commonsSpecialCharacterRemover(specialCharacterStr);
        return specialCharacterStr;
    }

}

 

公司评论这块不仅需要判断敏感字还需要对内容做一些特殊处理,今天整理下,以后可以留着用