Java模块 -- String字符串操作(数字,汉字,特殊符号过滤/截取)

来源:互联网 发布:小型公司网络规划方案 编辑:程序博客网 时间:2024/06/05 01:17

使用正则表达式,截取String字符串中的数字、汉字,以及过滤特殊符号


    /**     * 提取字符串中的数字     *     * @param number     * @return     * @throws Exception     */    public String numberIntercept(String number) throws Exception {        return Pattern.compile("[^0-9]").matcher(number).replaceAll("");    }    /**     * 提取字符串中所有的汉字     *     * @param str     * @return     * @throws Exception     */    public String intercept(String str) throws Exception {        String regex = "[\u4E00-\u9FA5]";//汉字        Matcher matcher = Pattern.compile(regex).matcher(str);        StringBuffer sb = new StringBuffer();        while (matcher.find()) {            sb.append(matcher.group());        }        return sb.toString();    }    /**     * 过滤设置的特殊符号     *     * @param str     * @return     * @throws Exception     */    public String filtration(String str) throws Exception {        String regEx = "[`~!@#$%^&*()+=|{}:;\\\\[\\\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]";        return Pattern.compile(regEx).matcher(str).replaceAll("").trim();    }


0 0
原创粉丝点击