当前位置:网站首页>Use of split() method in string class

Use of split() method in string class

2022-06-13 00:15:00 Tomatoes are sleepy

String Class split Use of methods


see api Will find ,split Method has method overload
One is split(String regex) One is split(String regex,int limit)

First explain split(String regex,int limit)
First look at the official explanation :
 Insert picture description here

Official explanation :
public String[] split(String regex,
int limit) Split this string into the given regular expression The matching of . The array returned by this method contains each substring of this string , The string is terminated by another substring that matches the given expression , Or terminated by the end of the string .
The substrings in the array are arranged in their order in this string . If the expression does not match any part of the input , The generated array has only one element , That is, this string .

When there is a positive width match at the beginning of this string , Contains an empty leading substring at the beginning of the result array . Zero width matching at the beginning will not produce such an empty leading substring .

limit The parameter controls the number of times the mode is applied , Therefore, the length of the generated array is affected . If the limit n Greater than 0, The mode can be applied at most n -1 Time , The length of the array is not greater than n
, The last entry of the array will contain all inputs beyond the last matching delimiter . If n Right and wrong , Then the pattern will be applied as many times as possible , And the array can have any length .
If n by 0, The pattern will be applied as many times as possible , Arrays can have any length , And the trailing empty string will be discarded .

understand : Is to satisfy the string regex The part of the string is replaced by an empty string and split from here , And convert it into an array

String Regex Is a regular expression ,limit Is the number of cuts

Now? limit There are three situations :1. Positive numbers 2. negative 3. be equal to 0

Positive numbers :

split Method will cut the string (limit-1) Time , That is to say, if limit =
1, that split The array length is 1, Is the original string , It just becomes an array . And that is regex Does not satisfy any characters in the string , It will also convert the original string into an array , If it is 2 Words , Then it will cut 1 Time , If it is 3 Words , cutting 2 Time

 Insert picture description here
 Insert picture description here
 Insert picture description here
 Insert picture description here

negative :

split Will cut an infinite number of times , That is, all the strings that satisfy regex Replace all with empty strings , Converted to an array
Whether it's -1 still -10, The result is the same

 Insert picture description here
 Insert picture description here

0:

split It also cuts an infinite number of times , Also, all the strings that satisfy regex Replace all with empty strings , But the empty string at the end of the string will be cleared and converted into an array

 Insert picture description here
 Insert picture description here
Look again. split(String regex), Is a default limit yes 0 Of split(String regex,int limit)

Yes split() The understanding of the method is a step deeper ,nice

原网站

版权声明
本文为[Tomatoes are sleepy]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/164/202206130010323457.html