java - String#split behavior when splitting the empty string according the "\s+" -
i newbie in java. unfortunately, in java there lot of stuff hard understand newbie.
for example,
string str = ""; string[] arr = str.split("\\s+"); system.out.println(arrays.tostring(arr)); system.out.println(arr.length); system.exit(0); the output is
[] 1 but why? appreciate if can explain me why length of array 1.
let's take @ implementation of arrays.tostring:
public static string tostring(object[] a) { if (a == null) return "null"; int imax = a.length - 1; if (imax == -1) return "[]"; stringbuilder b = new stringbuilder(); b.append('['); (int = 0; ; i++) { b.append(string.valueof(a[i])); // let's print that! if (i == imax) return b.append(']').tostring(); b.append(", "); } } now since array not empty, we'll for loop. begin appending [ result. append string.valueof(a[i]). let's try print it:
string str = ""; string[] arr = str.split("\\s+"); system.out.println(string.valueof(arr[0])); you'll see output is.. nothing! final result is:
[] please note has nothing java 8. split method introduced much earlier.
Comments
Post a Comment