Confusing output from String.split

前端 未结 8 1006
情深已故
情深已故 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:11

    Documentation:

    For: System.out.println("Output1: "+blank.split(",").length);

    The array returned by this method contains each substring of this string that is terminated by another substring that matches the given expression or is terminated by the end of the string. The substrings in the array are in the order in which they occur in this string. If the expression does not match any part of the input then the resulting array has just one element, namely this string.

    It will simply return the entire string that's why it returns 1.


    For the second case, String.split will discard the , so the result will be empty.

    String.split silently discards trailing separators
    

    see guava StringsExplained too

    0 讨论(0)
  • 2020-12-08 07:11

    We can take a look into the source code of java.util.regex.Pattern which is behind String.split. Way down the rabbit hole the method

    public String[] split(CharSequence input, int limit)
    

    is invoked.

    Input ""

    For input "" this method is called as

    String[] parts = split("", 0);
    

    The intersting part of this method is:

      int index = 0;
      boolean matchLimited = limit > 0;
      ArrayList<String> matchList = new ArrayList<>();
      Matcher m = matcher(input);
    
      while(m.find()) {
        // Tichodroma: this will not happen for our input
      }
    
      // If no match was found, return this
      if (index == 0)
        return new String[] {input.toString()};
    

    And that is what happens: new String[] {input.toString()} is returned.

    Input ","

    For input ","the intersting part is

        // Construct result
        int resultSize = matchList.size();
        if (limit == 0)
            while (resultSize > 0 && matchList.get(resultSize-1).equals(""))
                resultSize--;
        String[] result = new String[resultSize];
        return matchList.subList(0, resultSize).toArray(result);
    

    Here resultSize == 0 and limit == 0 so new String[0] is returned.

    0 讨论(0)
提交回复
热议问题