去掉空格的工具类

public class StringUtils extends org.apache.commons.lang3.StringUtils {
    
    public static String escapeSql(String param) {
        if (isEmpty(param)) {
            return param;
        }
        // 单引号是oracle字符串的边界,oralce中用2个单引号代表1个单引号
        String escape = param.replaceAll("'", "''");
        // 由于使用了/作为ESCAPE的转义特殊字符,所以需要对该字符进行转义
        // 这里的作用是将"a/a"转成"a//a"
        escape = escape.replaceAll("/", "//");
        // 使用转义字符 /,对oracle特殊字符% 进行转义,只作为普通查询字符,不是模糊匹配
        escape = escape.replaceAll("%", "/%");
        // 使用转义字符 /,对oracle特殊字符_ 进行转义,只作为普通查询字符,不是模糊匹配
        escape = escape.replaceAll("_", "/_");
        return escape;
    }

    
    @SuppressWarnings("unchecked")
    public static <T> T trimObject(T t) {
        if (t instanceof String) {
            return (T) trim((String) t);
        }
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(t.getClass());
            PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor pd : pds) {
                if (pd.getPropertyType().equals(String.class)) {
                    String val = (String) pd.getReadMethod().invoke(t);
                    pd.getWriteMethod().invoke(t, trim(val));
                }
            }
        } catch (IntrospectionException e) {
            e.printStackTrace();
        }return t;
    }
}
str = .replaceAll("\\s*", "");

可以替换大部分空白字符, 不限于空格  \s 可以匹配空格、制表符、换页符等空白字符的其中任意一个

 

posted @ 2023-03-13 20:36  Bonnie_ξ  阅读(73)  评论(0编辑  收藏  举报