If I split "hello|" and "|hello" with "|" character, then I get one value for the first and two values for the second version.
String[] arr1 = new String("hello|").split("\\|");
String[] arr2 = new String("|hello").split("\\|");
System.out.println("arr1 length: " + arr1.length + "\narr2 length: " + arr2.length);
This prints out:
arr1 length: 1
arr2 length: 2
Why is this?
split(String regex)- This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.str.contains("foo") ? str.split("foo")[1] : ""in production and it crashed because of this