欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

[java]任意位置切分string  

程序员文章站 2024-01-15 17:00:34
...

源码:

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;
	}

}

运行结果:


[java]任意位置切分string