I love the java series --- [method] String.Split

hello Hello everyone, long time no see, is split method java Today we have to discuss, perhaps you have long known, but do you really know?

 We see it.

First we look at the most commonly used split () method is the method a single parameter

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

FML, this thing is not overloaded method call is another method, but also gave a default value is 0, then we have to see which method overloading

Copy the code
public String[] split(String regex, int limit) {
    // Try fast splitting without allocating Pattern object
        String[] fast = Pattern.fastSplit(regex, this,limit);
        if (fast != null) {
            return fast;
        }
        return Pattern.compile(regex).split(this, limit);
}
Copy the code

? ? ? This is what a bunch of things, no matter, since this override this method, we look at the bottom of the method how to use it

int limit? what is this? The following is the explanation API

Parameter control mode to limit the number of times, and therefore affects the length of the resulting array. If the limit is greater than n 0, the mode is most widely n - 1 times, the length of the array will not be greater than n, and a last array will contain all of the input beyond the last delimiter matching. If n is non-positive, then the pattern to be applied as many times, and the array may be any length. If n is 0, then the pattern to be applied as many times, the array may be any length, and a null-terminated string is discarded.

Are not they live in their own world view, no way to test their own chant.

 Note: Please note the following test test string changes

1. Default test method, the above code can be seen, the default method is only pass limit 0 only.

Do not talk nonsense, let's get started test ( note that we are a comma after the last test string is empty )

Copy the code
String str = "1,2,4,5,6,7,";
String[] strArr = str.split(",");
        
for(String s: strArr){
    System.out.println("分割:"+s);
}
System.out.println ( "array length:" + strArr.length);
Copy the code

Output:

Copy the code
Division: 1
Split: 2
Split: 4
Split: 5
Split: 6
Split: 7
Array length: 6
Copy the code

Test results: According to the theory division, then the last one, "" the back of the empty should not also be segmented Well, but found that the test results last space is removed, so it is eliminating yet?

                  1. Remove the last space is not divided.

If so, then I can get more space in the back to remove it?

To test

Copy the code
        String str = "1,2,4,5,6,7,,,";
        String[] strArr = str.split(",");
        
        for(String s: strArr){
            System.out.println("分割:"+s);
        }
        System.out.println ( "array length:" + strArr.length);
Copy the code

Output:

Copy the code
Division: 1
Split: 2
Split: 4
Split: 5
Split: 6
Split: 7
Array length: 6
Copy the code

Test result: It seems limit is 0 if it is the last division of space, then no matter how many spaces will be removed

Other guess: 1. If the space appears in the middle will get rid of it? (Such as: 1,2,3, 4,5,6,7)

                  2. If the beginning of the emergence of a space will remove it? (Eg: ,, 1,2,3,4,5,6,7)

I will not put the code: 1 will not get rid of the middle of the space, the space will be used as a string into an array of the divided.

                             2. The beginning of the spaces will not be removed, do the reservations.

Conclusion: When the limit == 0, only the presence of the last spaces are removed, the other will not be removed in the middle and front.

2. When the limit> 0 beginning of the test.

Copy the code
        String str = "1,2,4,5,6,7";
        String[] strArr = str.split(",",5);
        
        for(String s: strArr){
            System.out.println("分割:"+s);
        }
        System.out.println ( "array length:" + strArr.length);
Copy the code

Output

Division: 1
Split: 2
Split: 4
Split: 5
Division: 6,7
Array length: 5

What? The last split out a "6,7"? ? ? ? ? Hell ah?

Do not worry we look at the number of division here is not equal to the number of filled limit?

Sure enough, that this limit is equal to the number of division is simple

Test results: limit equal to the number of copies you want to split.

Other guess: 1. If the limit> Copies largest division (such as "1,2,3,4,5,6,7" The biggest division is actually seven, which is greater than the maximum division is greater than 7) what will happen ?

                  2. If the limit is fractional how to do?  

Conclusion: it will only split if more than 1 in accordance with the maximum number of divisions.

           2.limit is an integer, you can not use decimals

2. When the limit <0 beginning of the test.

Copy the code
        String str = "1,2,4,5,6,7,";
        String[] strArr = str.split(",",-1);
        
        for(String s: strArr){
            System.out.println("分割:"+s);
        }
        System.out.println ( "array length:" + strArr.length);
Copy the code

 Output:

Copy the code
Division: 1
Split: 2
Split: 4
Split: 5
Split: 6
Split: 7
Split:
Array length: 7
Copy the code

 Haha interesting to see, and he did not last space shield

Do not buy off the back of the child

Conclusion: If the limit is negative, it will not remove any characters after the split, as 1 and -2 What is the difference, I still do not understand.

This article reprinted from: https://www.cnblogs.com/woaixingxing/p/10838546.html , fear of the future can not be found, it is copied, and write well, I will not write again, and here point to the author awesome!

Guess you like

Origin www.cnblogs.com/hujunwei/p/12343989.html