package com.test.util;
import java.util.ArrayList;
import java.util.List;
public class StringTest {
public static void main(String[] args) {
String aa="dfgyhujiklredftghjklrdkldfghjklereedwefghjqwjkwfghjrk32er32t23de4rftgysdf";
List<String> strList = getStrList(aa, 4);
System.out.println(strList.toString());
}
/**
*
* @param inputString 需要分割字符串
* @param length 分组长度
* @return
*/
public static List<String> getStrList(String inputString, int length) {
int size = inputString.length() / length;
if (inputString.length() % length != 0) {
size += 1;
}
return getStrList(inputString, length, size);
}
public static List<String> getStrList(String inputString, int length, int size) {
List<String> list = new ArrayList<String>();
for (int index = 0; index < size; index++) {
String childStr = substring(inputString, index * length, (index + 1) * length);
list.add(childStr);
}
return list;
}
public static String substring(String str, int f, int t) {
if (f > str.length())
return null;
if (t > str.length()) {
return str.substring(f, str.length());
} else {
return str.substring(f, t);
}
}
}
java把一个字符串等段分割并转换成集合数组
于 2023-12-05 16:31:23 首次发布