String.split () the most detailed source code interpretation and precautions

foreword

The blogger has entered the source code of the String class to look at the various empty string problems that occur during string segmentation, and now makes the following interpretation and demonstration:

One, split (regex, limit)

The first is the split method with two parameters:
insert image description here

effect:

Separate the string with the given regular expression (regex)

  • The first parameter is the delimiter of the incoming character type , such as "," etc. (can be any string)
  • The second parameter passes in the integer limit, which means that the string is divided into n parts (here n is the limit).

return value:

The array returned by this method contains each substring of this string that ends with the matched regular expression (that is, the first parameter regex entered) or ends with the end of the string.

Precautions:

  1. The substrings in the array are sorted in the order they appear in this string.
  2. If the input regex does not match any characters in the string , then the resulting array has only one element, which is the string. (That is, if there is no input regex parameter in the string)
  3. If there is a positive number of matches at the beginning of the string (that is, there are >0 regex delimiters at the beginning of the string), then an empty leading substring will be included at the beginning of the resulting array .
public class test {
    
    
    public static void main(String[] args) {
    
    
        String str = ",,1,2,3,4"; // 注意这里字符串开头就匹配到了逗号
        String[] s = str.split(",",10);// 这里先取10,后文介绍第二个参数
        for (String string : s) {
    
    
            System.out.println("子字符串"+string);
        }
        System.out.println(s.length);
    }

}

operation result:

An empty substring will appear before the first comma

insert image description here

  1. The limit parameter controls how many times the pattern is applied and therefore affects the length of the resulting array . (This means that the value of limit controls the length of the result array)
    insert image description here
    The interpretation of the above is as follows:
  • (1) If the limit input is a positive number , then the mode will apply limit - 1 times at most (That is to say, only the input regex will be used to match limit-1 times in the string), the length of the array will be no greater than limit, and the last entry of the array will contain all input except the last matched delimiter (That is to say, the pattern of his separation is from front to back). Give a code for everyone to understand:
public class test {
    
    
    public static void main(String[] args) {
    
    
        String str = "1,2,3,4";
        String[] s = str.split(",",2);//这里输入limit为2,即分成2部分
        for (String string : s) {
    
    
            System.out.println("子字符串"+string);
        }
        System.out.println(s.length);
    }

}

operation result:

The string is split into 2 substrings, the split pattern is from front to back

insert image description here

  • (2) If the input limit is zero , the pattern will be applied as many times as possible, the resulting array can have any length, and trailing empty strings will be discarded . (It is to match all the regex delimiters in the string), regarding the empty string being discarded, the code is as follows:
public class test {
    
    
    public static void main(String[] args) {
    
    
        String str = "1,2,3,4,,,";// 这里后面逗号之间的空字符串将被丢弃
        String[] s = str.split(",",0);
        for (String string : s) {
    
    
            System.out.println("子字符串"+string);
        }
        System.out.println(s.length);
    }

}

operation result:

Trailing empty strings will not appear in the resulting array

insert image description here

  • (3) If the value of the input limit is negative , the pattern will be applied as many times as possible, and the array can have any length. (The trailing empty string will not be lost either.)
public class test {
    
    
    public static void main(String[] args) {
    
    
        String str = ",1,2,3,4,";
        String[] s = str.split(",",-1);//limit值为负数
        for (String string : s) {
    
    
            System.out.println("子字符串"+string);
        }
        System.out.println(s.length);
    }

}

operation result:

trailing empty strings are not lost

insert image description here

Two, split (regex)

Then the split method with only one parameter is easy, that is, the default limit value is 0 .

insert image description here
The working principle of this method is to call the split method of two parameters with the given regex parameter and a limit parameter defaulting to 0. Therefore, the trailing empty string is not included in the resulting array.

insert image description here

Summarize

The above is the interpretation of the source code of the split method in the String class and all the precautions. It is purely hand-written. If it is helpful, please give it a follow + like and collect it ♥♥♥

Guess you like

Origin blog.csdn.net/qq_43575801/article/details/125946104