emoji表情符处理替换成空格

时间:2022-04-15 04:48:20
/**
   * 用filterOffUtf8Mb4
   * Description: 过滤率四个字节的utf-8字符(emoji表情符),替换成四个空格。
   *         四字节utf-8字符mysql存储报错 
   * @Version1.0
   * @param s
   * @return
   * @throws UnsupportedEncodingException 
   * @throws Exception
   */
  public static String parseUtf8(String s) throws UnsupportedEncodingException {
    byte[] b = s.getBytes("utf-8");
    for(int i=0, len=b.length; i<len; i++){
      if ((b[i] & 0xF8) == 0xF0) {//F8=1111 1000,F0=1111 0000过滤4位UTF8编码(字头11110xxx)
        b[i++] = 32;
        b[i++] = 32;
        b[i++] = 32;
        b[i++] = 32;
      }
      else if ((b[i] & 0xFC) == 0xF8) {//FC=1111 1100,F8=1111 1000过滤5位UTF8编码(字头111110xx)
        b[i++] = 32;
        b[i++] = 32;
        b[i++] = 32;
        b[i++] = 32;
        b[i++] = 32;
      }
      else if ((b[i] & 0xFE) == 0xFC) {//FE=1111 1110,FC=1111 1100,过滤6位UTF8编码(字头1111110x)
        b[i++] = 32;
        b[i++] = 32;
        b[i++] = 32;
        b[i++] = 32;
        b[i++] = 32;
        b[i++] = 32;
      }
    }
    return new String(b, "utf-8");

  }