StringUtils

public class StringUtils {

    /*** 字符串是否为空 */
    public static boolean isNotBlank(Object object) {
        if (object == null) {
            return false;
        } else if (object.toString().isEmpty()) {
            return false;
        } else if (object.toString().trim().isEmpty()) {
            return false;
        }
        return true;
    }

    /*** 字符串每隔count个字符加入separator分隔符*/
    public static String joinChar(int count, String source, String separator) {
        String regex = "(.{" + count + "})";
        source = source.replaceAll(regex, "$1" + separator + "");
        return source;
    }

    /***  以某个字符拆分字符串 */
    public static String[] splitChar(String source, String separator) {
        return source.split(separator);
    }

    /***  字符串source中获取某个字符ch第n次出现的位置index */
    public static int findAppearIndexOfN(String source, String ch, int n) {
        n = n - 1;
        //获取字符第一次出现的位置
        int x = source.indexOf(ch);
        for (int i = 0; i < n; i++) {
            x = source.indexOf(ch, x + 1);
        }
        return x;
    }

    /***  字符串source中获取某个字符ch出现的次数 */
    public static int findAppearCount(String source, String ch) {
        int n = 0;
        int x = source.indexOf(ch);
        if (x == -1) {
            //未检索到字符
            return 0;
        }
        while (x > -1) {
            n++;
            x = source.indexOf(ch, x + 1);
        }
        return n;
    }

    /***  将字符串source中长度为len索引为index的字符替换成目标字符target */
    public static String replace(String source, String target, int index, int len) {
        String goal = null;
        try {
            goal = new StringBuffer(source).replace(index, index + len, target).toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return goal;
    }

    /*** 从字符串source中随机获取一个字符 */
    public static String getRandomChar(String source) {
        if (isNotBlank(source)) {
            int i = RandomUtils.genRandomOfMN(0, source.length() - 1);
            return source.substring(i, i + 1);
        } else {
            ProcessException.exe(new ExceptionPojo("", "空指针异常"));
        }
        return null;
    }

    /*** 从字符串source中随机获取len个字符组成字符串 */
    public static String getLenString(String source, Integer len) {
        return AlgorithmUtils.getLenString(source, len);
    }

    public static void main(String[] args) throws Exception {

        String[] splitChar = splitChar("a,b,c,,", ",");
        for (String s :splitChar) {
            System.out.println(s);
        }


        System.out.println(splitChar[splitChar.length-1]);

    }
}

你可能感兴趣的:(工具类系列)