Java is about split handling empty strings and intercepting the maximum number.

  There is a split method in the String class of java, which we often use.


 There are two methods of split, 

 ①.public String[] split(String regex,int limit) 


 ②. public String[] split(String regex ) 


  Let's talk about the first one, there are two parameters, the example on the api:


public String[] split(String regex, int limit) 

        Splits this string based on matching the given regular expression. 
        
        The array returned by this method contains each substring of this string terminated by another substring matching the given expression or terminated by end of string. 
        
        The substrings in the array are in the order they are in this string. If the expression does not match any part of the input, the resulting array has only one element, this string. 

         The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit n is greater than 0, the pattern will be applied at most n - 1 times, the length of the array will be no greater than n, and the last item of the array will contain all input beyond the last matched delimiter. 


        If n is non-positive, the pattern will be applied as many times as possible, and the array can be of any length. If n is zero, the pattern will be applied as many times as possible, the array can be of any length, and trailing empty strings will be discarded. 

       

         For example, the string "boo:and:foo" with these parameters produces the following results: 


             Regex Limit results 

                :         2         { "boo", "and:foo" } 

                :         5         { "boo", "and", "foo" } 

                :        -2         { "boo", "and", "foo" } 

                o       5         { "b", "", ":and:f", "", "" } 

                o      -2         { "b", "", ":and:f", "", "" } 

                o 0 { "b", "", ":and:f" } 


        A method call of the form str.split(regex, n) produces exactly the same result as the following expression: 
        Pattern.compile(regex).split( str, n) 

        parameters: 
            regex - delimited regular expression 
            limit - result threshold, as described above 

        Returns: 
            an array of strings, which splits this string according to the matches of the given regular expression, resulting in this array 


        throws: 

            PatternSyntaxException - if the syntax of the regular expression is invalid 


 In fact, the above is already very detailed. In the final analysis, there is one more limit parameter than the commonly used one. But how to use parameters.


        1. When the parameter is an integer, only the first few need to be intercepted, and a few need to be intercepted. Needless to say. 

       String line = "aa,bb,cc,dd,,,,";  
       System.out.println(line.split(",",6).length);  
       The output result is 6, the limit parameter specifies how many, and how many output up to 8  


        2. When the parameter is zero, like split(), screenshot as many strings as possible (in fact, not the most). 

        String line = "aa,bb,cc,dd,,,,";  
        System.out.println(line.split(",",0).length);  
        The output is 4  


        3. When the parameter is negative, even if there is an empty string behind it, it will output to the maximum 

        String line = "aa,bb,cc,dd,,,,";  
        System.out.println(line.split(",",-1).length);  
        The output result is 8.  


        Maybe everyone can see it. The comma-cut files are generally files in CSV format. When reading each line of records without adding parameters, this situation will occur, and the reading is incomplete. Then, when the CSV data format is verified, the verification data is not legitimate. 

       String line = "aa,bb,cc,dd,,,,";      
       String mi[] = (line).split(sp);           
       if (mi.length != 8)return;  

       The actual result mi.length = 4, so the direct illegal data cannot pass the verification  



        ②In fact, it is the result obtained when the limit parameter is 0. I will not say more about the introduction of the api. 

        public String[] split(String regex) 


        Splits this string according to a match of the given regular expression. 


        This method acts as if the two-argument split method was called with the given expression and a limit argument of 0. Therefore, the trailing empty string is not included in the resulting array. 


        For example, the string "boo:and:foo" produces a result with these expressions: 


        Regex result 
        : { "boo", "and", "foo" } 

        o         { "b", "", ":and:f" } 



        Parameters: 
            regex - the delimited regular expression 


        Returns: 
            an array of strings, resulting from splitting this string based on matches of the given regular expression. 


        throws: 

            PatternSyntaxException - if the syntax of the regular expression is invalid 



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324855369&siteId=291194637