-2
String str = "1,2,3,,";
String[] arr1 = str.split(",");
String[] arr2 = str.split(",", -1);
// equals ?false
System.out.println(Arrays.deepEquals(arr1, arr2));
// values [1, 2, 3]
System.out.println(Arrays.deepToString(arr1));
// [1, 2, 3, , ]
System.out.println(Arrays.deepToString(arr2));
我應該選擇哪種方法?給我一些建議。String方法split(String str,int len)和split(String str)之間有什麼區別?
祕密被埋在API中:https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#split-java.lang.String-/https:// docs.oracle.com/javase/8/docs/api/java/lang/String.html#split-java.lang.String-int- –
您需要選擇的方法取決於您要使用結果的方式。 –