String帮助类(字符串工具类、验证邮箱、电话、手机号码;验证某个字符是否为数字、是否为数字、替换空格、换行符等不可见字符等等)

package com.cnksi.utils;

import com.jfinal.kit.StrKit;

import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {

    /**
     * 替换空格、换行符等不可见字符
     *
     * @param str
     * @return
     */
    public static String replaceSpace(String str) {
        String result = str;
        if (str != null && str.length() > 0) { // 替换空格、换行符等不可见字符
            String regEx = "\\s| {1,}";
            Pattern pattern = Pattern.compile(regEx);
            Matcher m = pattern.matcher(str);

            result = m.replaceAll("").trim();
        }
        return result;
    }

    // /**
    // * 判断一个字符串是否为纯文本数字
    // */
    // public static boolean isNumeric(String str) {
    // Pattern pattern = Pattern.compile("[0-9]{1,}"); // 数字至少出现1次
    // Matcher isNum = pattern.matcher(str);
    // if (!isNum.matches()) {
    // return false;
    // }
    // return true;
    // }

    /**
     * 是否为数字
     *
     * @return
     */
    public static boolean isNumeric(String str) {
        if (str == null || str.isEmpty()) {
            return false;
        }
        char[] arr = str.toCharArray();
        for (char c : arr) {
            if (!Character.isDigit(c)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 验证某个字符是否为数字
     *
     * @param str
     * @return
     */
    public static boolean isNumber(String str) {
        if (isBlank(str)) {
            return false;
        }
        // 该正则表达式可以匹配所有的数字 包括负数
        Pattern pattern = Pattern.compile("-?[0-9]+\\.?[0-9]*");
        String bigStr;
        try {
            bigStr = new BigDecimal(str).toString();
        } catch (Exception e) {
            return false;//异常 说明包含非数字。
        }

        Matcher isNum = pattern.matcher(bigStr); // matcher是全匹配
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }

    /**
     * 如果字符串为空,返回空字符串,否则调用trim方法
     *
     * @param str
     * @return
     */
    public static String trimToEmpty(String str) {
        return str == null ? "" : str.trim();
    }

    /**
     * 如果字符串不为空,调用trim方法
     *
     * @param str
     * @return
     */
    public static String trimToNull(String str) {
        return str == null ? null : str.trim();
    }

    /**
     * 调用对象的toString()方法,如果对象为 null 返回空字符串("")
     *
     * @param o
     * @return
     */
    public static String toStr(Object o) {
        return o == null ? "" : o.toString();
    }

    public static String replaceBlank(String str) {
        if (str != null) {
            str = str.replaceAll("\\s*|\t|\r|\n", "");
        }
        return str;
    }

    public static boolean isEmpty(String str) {
        return str == null || str.isEmpty();
    }

    public static boolean notEmpty(String str) {
        return !isEmpty(str);
    }

    public static boolean notBlank(String str) {
        return str == null || "".equals(str.trim()) || "null".equals(str) ? false : true;
    }

    /**
     * 是否全都为空
     *
     * @param str
     * @return
     */
    public static boolean isBlank(String... str) {
        for (String s : str) {
            if (notBlank(s))
                return false;
        }
        return true;
    }

    /**
     * 是否至少有一个为空
     *
     * @param str
     * @return
     */
    public static boolean isBlankOneLeast(String... str) {
        for (String s : str) {
            if (isBlank(s))
                return true;
        }
        return false;
    }

    /**
     * @param offset 从第几位分割
     * @param str    需要分割的字符串
     * @param symbol 需要用什么分割
     * @return 返回分割后的字符串
     */
    public static String insertStrToString(int offset, String str, String symbol) {
        if (!StrKit.isBlank(str)) {
            StringBuilder sb = new StringBuilder(str);
            for (int i = offset; i < sb.length(); i += offset) {
                sb.insert(i++, symbol);
            }
            return sb.toString();
        }
        return "";
    }

    /**
     * 去掉首位字符(逗号等)
     *
     * @param str
     * @param c   去掉的字符
     */
    public static String trimChar(String str, String c) {
        if (isBlank(str) || isBlank(c))
            return trimToNull(str);

        int len = c.length();
        // 去掉之前的字符
        if (str.startsWith(c)) {
            str = str.substring(len);
        }

        // 去掉之后的字符
        if (str.endsWith(c)) {
            str = str.substring(0, str.length() - len);
        }

        return str;
    }

    /**
     * 多个逗号转换为单个逗号 并去掉首尾逗号
     *
     * @param s
     * @return
     */
    public static String dealWithComma(String s) {
        if (s.length() > 0) {
            s = s.replaceAll("[',']+", ",");// 多个,,,,替换为,
            // 去掉首尾,
            if (s.startsWith(",")) {
                s = s.substring(1);
            }
            if (s.endsWith(",")) {
                s = s.substring(0, s.length() - 1);
            }
        }
        return s;
    }

    /**
     * String[] 拼接字符串方法 包含引号
     *
     * @param s
     * @return 's1','s2','s3'
     */
    public static String genStrWithQuote(String[] s) {
        String re = "";
        for (String tmp : s) {
            re = re + "'" + tmp + "',";
        }
        return dealWithComma(re);
    }

    /**
     * String[] 拼接字符串方法 不含引号
     *
     * @param s
     * @return s1, s2, s3
     */
    public static String genStrNoQuote(String[] s) {
        return genStrWithQuote(s).replace("'", "");
    }

    /**
     * 因为系统内部存在字符串拼接SQL语句,并未使用占位符,所以需手动过滤特殊字符防止SQL注入
     *
     * @param str
     * @return
     */
    public static String TransactSQLInjection(String str) {
        return str.replaceAll("([';])+|(--)+", "");

    }

    /**
     * 获取文件名称后缀
     *
     * @param fileName 文件名称
     * @return 后缀
     */
    public static String getFileSuffix(String fileName) {
        if (isBlank(fileName) || !fileName.contains(".")) {
            return "";
        }
        return fileName.substring(fileName.indexOf(".") + 1, fileName.length());
    }

    /**
     * 手机号验证
     *
     * @param str
     * @return 验证通过返回true
     * @author :shijing
     * 2016年12月5日下午4:34:46
     */
    public static boolean isMobile(final String str) {
        Pattern p;
        Matcher m;
        boolean b;
        p = Pattern.compile("^[1][3,4,5,7,8][0-9]{9}$"); // 验证手机号
        m = p.matcher(str);
        b = m.matches();
        return b;
    }

    /**
     * 电话号码验证
     *
     * @param str
     * @return 验证通过返回true
     * @author :shijing
     * 2016年12月5日下午4:34:21
     */
    public static boolean isPhone(final String str) {
        Pattern p1, p2;
        Matcher m;
        boolean b;
        p1 = Pattern.compile("^[0][1-9]{2,3}-[0-9]{5,10}$");  // 验证带区号的
        p2 = Pattern.compile("^[1-9]{1}[0-9]{5,8}$");         // 验证没有区号的
        if (str.length() > 9) {
            m = p1.matcher(str);
            b = m.matches();
        } else {
            m = p2.matcher(str);
            b = m.matches();
        }
        return b;
    }

    /**
     * 验证是否为正确的邮箱号
     *
     * @param email 需要验证的邮箱地址
     * @return
     */
    public static boolean isValidEmail(String email) {
        // 1、\\w+表示@之前至少要输入一个匹配字母或数字或下划线 \\w 单词字符:[a-zA-Z_0-9]
        // 2、(\\w+\\.)表示域名. 如新浪邮箱域名是sina.com.cn
        // {1,3}表示可以出现一次或两次或者三次.
        String reg = "\\w+@(\\w+\\.){1,3}\\w+";
        Pattern pattern = Pattern.compile(reg);
        boolean flag = false;
        if (email != null) {
            Matcher matcher = pattern.matcher(email);
            flag = matcher.matches();
        }
        return flag;
    }

    public static void main(String[] args) {
        //System.out.println(isValidEmail("1@qq.cn"));
    }

    /**
     * 将字符(保留前3位,保留后四位,中间替换为 *)用于身份证或者电话、银行卡号等敏感信息显示
     *
     * @param str
     * @return
     */
    public static String replaceSensitiveStr(String str) {
        if (isBlank(str) || str.length() < 11)
            return str;
        str = str.replaceAll("(?<=[\\d]{3})\\d(?=([\\d]|[a-z]|[A-Z]){4})", "*"); //这里*只要一个,因为会替代多次,每次一个。
        return str;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值