在日常开发中难免会遇到处理字符串,其中可能会需要过滤特殊的字符,避免出错。

/**
 * @Author: Hyy
 * @Desc: 正则表达式过滤字符
 * @Date: 2021/9/28 20:56
 */
public class CharacterFilter {
    /**
     * 过滤出中文、数字、字母
     * @param str
     * @return
     */
    public static String stringFilter(String str) {
        return str.replaceAll("[^(a-zA-Z0-9\\\\u4e00-\\\\u9fa5)]","").trim();
    }

    /**
     * 过滤出中文
     * @param str
     * @return
     */
    public static String chineseFilter(String str) {
        return str.replaceAll("[^(\\u4e00-\\u9fa5)]","").trim();
    }

    /**
     * 过滤出数字
     * @param str
     * @return
     */
    public static String numberFilter(String str) {
        return str.replaceAll("[^0-9]","").trim();
    }
}

每天积累一点点,积沙成塔。

更多推荐

java使用正则表达式过滤特殊字符