public static void main(String[] args) { String s = "A1B2C3D4E5F6G7H8"; String[] arr1 = s.split("[1-8]"); String[] arr2 = s.split("[A-H]");// System.out.println(Arrays.toString(arr1));//[A, B, C, D, E, F, G, H] System.out.println(Arrays.toString(arr2));//[, 1, 2, 3, 4, 5, 6, 7, 8] int[] a = new int[8]; for (int i = 1; i < arr2.length; i++) { a[i-1] = Integer.valueOf(arr2[i]); } System.out.println(Arrays.toString(a)); System.out.println(arr2.length);//9 首个字母也被当做拆分符,所以,它前面有个空字符串 System.out.println(arr1.length);//8 }
时间: 2024-09-30 04:52:32