分割成数组
import java.util.Arrays;/*** Java将字符串按照指定长度分割成字符串数组*/
public class StringUtils {public static void main(String[] args){String data = "227d77a7a244c7b2be3180f2d46be352f56ddf92866692f2cac797358097e5a3e90f6d20bb96bc516a4ab9c09c84ab3c05b5503a7b2c7529ee2691d5deb44b12";System.out.println(Arrays.toString(stringToStringArray(data, 50)));}private static String[] stringToStringArray(String src, int length){// 检查参数是否合法if(null == src || src.equals("")){return null;}if(length <= 0){return null;}// 获取整个字符串可以被切割成字符子串的个数int n = (src.length() + length - 1) / length;String[] split = new String[n];for(int i = 0; i < n ; i++){if(i < (n - 1)){split[i] = src.substring(i * length, (i + 1) * length);} else {split[i] = src.substring(i * length);}}return split;}
}
# 控制台输出:
[227d77a7a244c7b2be3180f2d46be352f56ddf92866692f2ca, c797358097e5a3e90f6d20bb96bc516a4ab9c09c84ab3c05b5, 503a7b2c7529ee2691d5deb44b12]
分割成list
public static List<String> splitStringByLength(String input, int length) {List<String> result = new ArrayList<>();for (int i = 0; i < input.length(); i += length) {result.add(input.substring(i, Math.min(input.length(), i + length)));}return result;}
参考来源:
Java将字符串按照指定长度分割成字符串数组 - 简书