Home >Java >javaTutorial >How Does Java 8's `String.split()` Handle Empty Strings Differently Than Java 7?
In Java 8, the behavior of String.split() has undergone changes compared to its predecessor, Java 7. Specifically, the way empty strings are handled at the beginning of the resulting array has been modified.
Prior to Java 8, split() would generate an array with empty strings at the beginning and end if the delimiter was an empty string. These empty strings were eventually removed, leaving only the non-empty strings.
In Java 8, the mechanism has changed. If the delimiter is an empty string, split() will produce an array with the empty strings at the beginning removed. However, this removal only applies to zero-width matches at the start of the input string.
The documentation for Pattern.split() in Java 8 explicitly states that an empty leading substring will be included only if there is a positive-width match at the beginning of the input string. If the match at the start is zero-width, no empty leading substring is included.
Examining the reference implementations between Java 7 and Java 8 reveals that the new condition is accounted for in Java 8. The code below shows the added logic:
if (index == 0 && index == m.start() && m.start() == m.end()) { // no empty leading substring included for zero-width match // at the beginning of the input char sequence. continue; }
To ensure consistent behavior across Java versions and be compatible with Java 8, consider the following:
(?!A) verifies that the match does not occur at the beginning of the string, effectively excluding zero-length matches at the start.
The above is the detailed content of How Does Java 8's `String.split()` Handle Empty Strings Differently Than Java 7?. For more information, please follow other related articles on the PHP Chinese website!