In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
Official account of Wechat
Segmenting long strings, encapsulating elements or combining encapsulated information into long strings is a routine operation, but such long strings are often uncontrollable. For example, a long string is composed of an element, or there is an empty string. This seems to be no problem, but the result of segmentation using split methods of different tool classes is completely different. I don't know if you have encountered such a pit, let's take a look.
The split method that comes with the String class
Directly use the split method that comes with String to see the segmentation results of different strings.
A string without a delimiter
/ / str = "1" public static void split2 (String str) {String [] splits = str.split (","); for (String split: splits) {System.out.println ("- >" + split);} System.out.println ("array length:" + splits.length);}
The result is a single element ["1"] and the array length is 1.
There is a delimiter, and there is no empty string after the delimiter is split
/ / str = "1J2" / / the code is the same as above.
The result is ["1", "2"] and the length of the array is 2. (assume that the number of delimiters is n and the number of elements after division is 1)
There is a delimiter, and an empty string appears after the delimiter is split
/ / str = "1Jing Jue 2" / / the code is the same as above.
The result is ["1", "", "2"] and the length of the array is 3. (assume that the number of delimiters is n and the number of elements after division is 1)
In the final summary, no matter how many delimiters there are, the result of the final segmentation is 1 element. It is also required by normal business logic. However, there is a problem. If the string passed in is null, a NullPointException exception will be reported, which requires an additional layer of non-null judgment.
StringUtils utility classes provided by commons
The specific fully qualified class name is org.apache.commons.lang3.StringUtils, which is arguably the most commonly used utility class for a string. Such as judging non-null, non-empty strings, or non-spaces, using the isNotBlank method, using collection elements to assemble strings, using the join method, and so on. Now let's look at how the split method is implemented in different situations.
A string without a delimiter
/ / str = "1" public static void split1 (String str) {String [] splits = org.apache.commons.lang3.StringUtils.split (str, ","); for (String split: splits) {System.out.println ("- >" + split);} System.out.println ("array length:" + splits.length);}
The result is a single element ["1"] and the array length is 1.
There is a delimiter, and there is no empty string after the delimiter is split
/ / str = "1J2" / / the code is the same as above.
The result is two elements ["1", "2"] and the array length is 2.
There is a delimiter, and an empty string appears after the delimiter is split
/ / str = "1Jing Jue 2" / / the code is the same as above.
The result after sharding is the same as in the previous case, with two elements ["1", "2"] and the array length is 2.
You can see the problem here. When an empty string occurs, the empty string is automatically discarded, leaving only the non-empty string. However, it is important to note that if it is a space, it will also form an element. For example, now the str input is "1, 2", and there is a space between the two delimiters, which will be split into three elements.
StringUtils utility classes provided by the util package under Spring
Fully qualified class name org.springframework.util.StringUtils, this StringUtils is also often used, because when typing StringUtils, IDEA automatically prompts, often it is in the first place. So how is it different from the above two methods in split?
A string without a delimiter
/ / str = "1" public static void split3 (String str) {String [] splits = org.springframework.util.StringUtils.split (str, ","); for (String split: splits) {System.out.println ("- >" + split);} System.out.println ("array length:" + splits.length);}
Sorry at this time, an NullPointerException exception will be reported, that is, the split method must have a delimiter.
There is a delimiter, and there is no empty string after the delimiter is split
/ / str = "1J2" / / the code is the same as above.
The result is two elements ["1", "2"] and the array length is 2.
There is a delimiter, and an empty string appears after the delimiter is split
/ / str = "1Jing Jue 2" / / the code is the same as above.
The result is two elements ["1", "2"] and the array length is 2. When you see here, you may think my array format is wrong, but in fact, the second element is indeed like this, no doubt.
This split method is still very crappy, it will find out whether the delimiter exists in the string, if there is no direct error, if it does, no matter how many delimiters there are, it is segmented with the first delimiter, resulting in two elements.
Summary
Three split methods and three different sharding logic, it is very fastidious when to split in that way, because there are a lot of inexplicable BUG in the development process. I've only met it twice.
The first time is to use the org.apache.commons.lang3.StringUtils to split the string, there is an empty string in the string, resulting in the number of elements after cutting is different from the actual number, exactly less elements, the result code when filling data into another string placeholder, there is always the phenomenon of dislocation. (for example, if the string that should have been placed on the placeholder 3 appears as an empty string, during segmentation, the element in position 3 is actually discarded, and the element in position 4 is placed in position 3.) for the second time, org.apache.commons.lang3.StringUtils is also used to split the string. The test has always been normal, but after testing for a period of time, it was found that it was reported to NullPointException, and it was only found after investigation. The introduction of StringUtils was caused by other developers replacing it with org.springframework.util.StringUtils. Because this string may not have a delimiter.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.