FULL PRODUCT VERSION :
java version "1.7.0_45"
Java(TM) SE Runtime Environment (build 1.7.0_45-b18)
Java HotSpot(TM) 64-bit Server VM (build 24.45-b08, mixed mode)
ADDITIONAL OS VERSION INFORMATION :
Linux 2.6.32-38-server #83-Ubuntu SMP Wed Jan 4 11:26:59 UTC 2012 x86_64 GNU/Linux
A DESCRIPTION OF THE PROBLEM :
Documentation of String's split(String regex,int limit) method says: "If [limit] is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded."
However if we try to split an empty string like this:
"".split(",",0)
the resulting array has length of 1 and contains one empty string.
Following the logic described by the documentation, the resulting array should have length of 0, because right after splitting, the last trailing string is empty, so it should be discarded.
This bug is easy to bypass, but still - such low-level method should work exactly as documentation says.
If this bug exists unnoticed in version as late as 1.7, I suspect it still made it into 1.8 - I haven't tested.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Simply split an empty string.
String[] splitted = "".split(",",0);
System.out.println(splitted.length);
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
public class SplitTest {
public static void main(String[] args){
String[] splitted = "".split(",",0);
System.out.println(splitted.length);
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Additional check right after splitting, substitution with an empty array. Not very elegant nor efficient.
public class SplitTest {
public static void main(String[] args){
String[] splitted = "".split(",",0);
if(splitted.length == 1 && splitted[0].length() == 0) splitted = new String[0];
System.out.println(splitted.length);
}
}
java version "1.7.0_45"
Java(TM) SE Runtime Environment (build 1.7.0_45-b18)
Java HotSpot(TM) 64-bit Server VM (build 24.45-b08, mixed mode)
ADDITIONAL OS VERSION INFORMATION :
Linux 2.6.32-38-server #83-Ubuntu SMP Wed Jan 4 11:26:59 UTC 2012 x86_64 GNU/Linux
A DESCRIPTION OF THE PROBLEM :
Documentation of String's split(String regex,int limit) method says: "If [limit] is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded."
However if we try to split an empty string like this:
"".split(",",0)
the resulting array has length of 1 and contains one empty string.
Following the logic described by the documentation, the resulting array should have length of 0, because right after splitting, the last trailing string is empty, so it should be discarded.
This bug is easy to bypass, but still - such low-level method should work exactly as documentation says.
If this bug exists unnoticed in version as late as 1.7, I suspect it still made it into 1.8 - I haven't tested.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Simply split an empty string.
String[] splitted = "".split(",",0);
System.out.println(splitted.length);
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
public class SplitTest {
public static void main(String[] args){
String[] splitted = "".split(",",0);
System.out.println(splitted.length);
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Additional check right after splitting, substitution with an empty array. Not very elegant nor efficient.
public class SplitTest {
public static void main(String[] args){
String[] splitted = "".split(",",0);
if(splitted.length == 1 && splitted[0].length() == 0) splitted = new String[0];
System.out.println(splitted.length);
}
}
- duplicates
-
JDK-8020672 String.split() return non-empty array on empty string
-
- Closed
-