开发者社区> 问答> 正文

大家一般怎么对内容中的一些无效字符做过滤呢?:配置报错 

我们一个应用,字符集是 UTF-8 的,但之前有导入一些内容包含了一些怪字符,这些字符本身不在 UTF-8 范畴之内的,例如: Apache Commons工具集简介 ��� 导致根据这个内容生成的 XML 文档或者是其他文本都不被编辑器所识别,一些XML解析库也会报XML格式错误的信息 大家有碰到这种问题一般是如何处理呢,现在想对这样的内容做过滤,也不知道该怎么过滤

展开
收起
kun坤 2020-06-02 14:59:20 594 0
1 条回答
写回答
取消 提交回答
  • Java 可以用如下方法来来过滤

    /** * Function to strip control characters from a string. * Any character below a space will be stripped from the string. * @param iString the input string to be stripped. * @return a string containing the characters from iString minus any control characters. */ public String stripControlChars(String iString) { StringBuffer result = new StringBuffer(iString); int idx = result.length(); while (idx-- > 0) { if (result.charAt(idx) < 0x20 && result.charAt(idx) != 0x9 && result.charAt(idx) != 0xA && result.charAt(idx) != 0xD) { if (log.isDebugEnabled()) { log.debug("deleted character at: "+idx); } result.deleteCharAt(idx); } } return result.toString(); }
    ######还可以试试正则表达式

    stringName.replaceAll("[^\\p{Print}]", "");
    ######乖乖,犀利######鉴客的正则耍得太犀利了。 受教了。######PHP的 http://php.net/manual/zh/function.urldecode.php######红薯又不干好事,扒人网页了吧######我的做法就是转码unicode###### 我导日文数据的时候,出现过这个问题,一般情况下是编码转换。
    2020-06-02 14:59:26
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载