java巨大字符串截取

2022-08-16 16:23:12 浏览数 (1)

stringlist,分段截取

比如传入rubenrubenruben和5,得到的list就是{"ruben","ruben","ruben"}这样的

代码语言:javascript复制
/**
     * @param initial  初始字符串
     * @param interval 分段长度
     * @return
     */
    public static List<String> stringSplit(String initial, Integer interval) {
        List<String> result = new LinkedList<>();
        StringBuilder tmp = new StringBuilder(initial);
        int length = tmp.length();
        while (length > 0) {
            if (interval > length) {
                interval = length;
            }
            String tmpStr = tmp.substring(0, interval);
            result.add(tmpStr);
            tmp.replace(0, interval, "");
            length = tmp.length();
        }
        return result;
    }

0 人点赞