JAVA inquiry string split method

Today in the use of split split the string suddenly thought of a situation, as follows:

String str="aaaaaaaab";
String arr[]=str.split("aa");

Ask, how much is the length of the array arr?
That if str is "baaaaaaaa" it

	String str="baaaaaaaa";

If str = "aaaaaaaab" it

String str="aaaaaaaab";

If str = "baaaaaaaab" it

String str="baaaaaaaab";

Well, we have to verify it in the program:

public class Test {

	public static void main(String[] args) {
		String str="aaaaaaaa";
		String [] arr=str.split("aa");
		System.out.println("字符串aaaaaaaa分割的数组长度为:"+arr.length);
		
		str="baaaaaaaa";
		arr=str.split("aa");
		System.out.println("字符串baaaaaaaa分割的数组长度为:"+arr.length);
		
		str="aaaaaaaab";
		arr=str.split("aa");
		System.out.println("字符串aaaaaaaab分割的数组长度为:"+arr.length);
		
		str="baaaaaaaab";
		arr=str.split("aa");
		System.out.println("字符串baaaaaaaab分割的数组长度为:"+arr.length);

	}
}

Run the above code is output
Here Insert Picture Description
to see the results you are not a little bit surprised, if any, it would continue to look down.
View source seen by split method in turn calls the split (regex, 0) method and pass a 0:

  public String[] split(String regex) {
        return split(regex, 0);
    }

Continue to view source code

 public String[] split(String regex, int limit) {
        /* fastpath if the regex is a
         (1)one-char String and this character is not one of the
            RegEx's meta characters ".$|()[{^?*+\\", or
         (2)two-char String and the first char is the backslash and
            the second is not the ascii digit or ascii letter.
         */
        char ch = 0;
        if (((regex.value.length == 1 &&
             ".$|()[{^?*+\\".indexOf(ch = regex.charAt(0)) == -1) ||
             (regex.length() == 2 &&
              regex.charAt(0) == '\\' &&
              (((ch = regex.charAt(1))-'0')|('9'-ch)) < 0 &&
              ((ch-'a')|('z'-ch)) < 0 &&
              ((ch-'A')|('Z'-ch)) < 0)) &&
            (ch < Character.MIN_HIGH_SURROGATE ||
             ch > Character.MAX_LOW_SURROGATE))
        {
            int off = 0;
            int next = 0;
            boolean limited = limit > 0;
            ArrayList<String> list = new ArrayList<>();
            while ((next = indexOf(ch, off)) != -1) {
                if (!limited || list.size() < limit - 1) {
                    list.add(substring(off, next));
                    off = next + 1;
                } else {    // last one
                    //assert (list.size() == limit - 1);
                    list.add(substring(off, value.length));
                    off = value.length;
                    break;
                }
            }
            // If no match was found, return this
            if (off == 0)
                return new String[]{this};

            // Add remaining segment
            if (!limited || list.size() < limit)
                list.add(substring(off, value.length));

            // Construct result
            int resultSize = list.size();
            if (limit == 0) {
                while (resultSize > 0 && list.get(resultSize - 1).length() == 0) {
                    resultSize--;
                }
            }
            String[] result = new String[resultSize];
            return list.subList(0, resultSize).toArray(result);
        }
        return Pattern.compile(regex).split(this, limit);
    }

There are shows where the relationship will eventually execute Pattern.compile (regex) .split (this, limit) this piece of code, down basis Pa Code:

  public String[] split(CharSequence input, int limit) {
        int index = 0;
        boolean matchLimited = limit > 0;
        ArrayList<String> matchList = new ArrayList<>();
        Matcher m = matcher(input);

        // Add segments before each match found
        while(m.find()) {
            if (!matchLimited || matchList.size() < limit - 1) {
                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;
                }
                String match = input.subSequence(index, m.start()).toString();
                matchList.add(match);
                index = m.end();
            } else if (matchList.size() == limit - 1) { // last one
                String match = input.subSequence(index,
                                                 input.length()).toString();
                matchList.add(match);
                index = m.end();
            }
        }

        // If no match was found, return this
        if (index == 0)
            return new String[] {input.toString()};

        // Add remaining segment
        if (!matchLimited || matchList.size() < limit)
            matchList.add(input.subSequence(index, input.length()).toString());

        // 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);
    }

By the code we can see the final matchList collection have value, but the value is empty then

  while (resultSize > 0 && matchList.get(resultSize-1).equals(""))
                resultSize--;

This piece of code, the first judge the last one is not empty, if there is no value, then it is a cut, and so on, so that we see the results appear on the above procedure is not is not surprising.
So we can boldly summarize, using the split method split the string, if the last few are empty, vacant position will be removed.
Welcome to the concern of my micro-channel public number yo
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/yu805894501/article/details/94651873