1 String str = " a b c d e f g c ";
2 String [] arr = str.split("\\s+");
3 System.out.println(Arrays.asList(arr));
4
5 for(String ss : arr){
6 System.out.println(ss+ss.length());
7 }
8 System.out.println("-------------------");
9 String [] arr2 = str.split(" ");
10 System.out.println(Arrays.asList(arr2));
11 for(String ss : arr2){
12 System.out.println(ss+ss.length());
13 }
14
15 輸出:
16 [, a, b, c, d, e, f, g, c]
17 0
18 a1
19 b1
20 c1
21 d1
22 e1
23 f1
24 g1
25 c1
26 -------------------
27 [, , , , a, , b, , , , , c, , d, e, f, , , , , , g, c]
28 0
29 0
30 0
31 0
32 a1
33 0
34 b1
35 0
36 0
37 0
38 0
39 c1
40 0
41 d1
42 e1
43 f1
44 0
45 0
46 0
47 0
48 0
49 g1
50 c1