Confusing output from String.split

前端 未结 8 1029
情深已故
情深已故 2020-12-08 06:33

I do not understand the output of this code:

public class StringDemo{              
    public static void main(String args[]) {
        String blank = \"\";         


        
8条回答
  •  甜味超标
    2020-12-08 07:05

    From String class javadoc for the public String[] split(String regex) method:

    Splits this string around matches of the given regular expression.

    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.

    In the first case, the expression does not match any part of the input so we got an array with only one element - the input.

    In the second case, the expression matches input and split should return two empty strings; but, according to javadoc, they are discarded (because they are trailing and empty).

提交回复
热议问题