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

根据传入字符串和截取个数分割字符串

程序员文章站 2024-01-24 11:42:58
...
package date0419;

public class Test01 {
	public static void main(String[] args) {
		String[] s=split("java4ever",2);
		for(int i=0;i<s.length;i++){
			System.out.print(s[i]+",");
		}
	}
	public static String[] split(String str, int chars) {
		String[] str1 = null;
		int len = (str.length() + chars - 1) / chars;
		str1 = new String[len];
		for (int i = 0; i < len; i++) {
			if (i < len - 1) {
				str1[i] = str.substring(i * chars, chars * (i + 1));
			} else {
				str1[i] = str.substring(i * chars);
			}
		}
		return str1;
	}
}