ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
源码: ~~~ package test1.string; import java.util.ArrayList; import java.util.List; public class Split { public static void main(String[] args) { for (String part : getParts("This is an example", 2)) { System.out.println(part); } } private static List<String> getParts(String string, int partitionSize) { List<String> parts = new ArrayList<String>(); int len = string.length(); for (int i = 0; i < len; i += partitionSize) { parts.add(string.substring(i, Math.min(len, i + partitionSize))); } return parts; } } ~~~ 运行结果: ![](https://box.kancloud.cn/2016-05-17_573adb3129a7f.jpg)