Preface
I believe everyone should know that in Java programming, sometimes we need to divide a string according to a specific character, letter, etc. as a cut-off point, so that we can use part of this string or save all the intercepted content into an array. The following article will share with you two ways of segmentation. Let’s take a look together.
1. The split() method of java.lang.String, JDK 1.4 or later
public String[] split(String regex,int limit)
Sample code
public class StringSplit { public static void main(String[] args) { String sourceStr = "1,2,3,4,5"; String[] sourceStrArray = sourceStr.split(","); for (int i = 0; i < sourceStrArray.length; i++) { System.out.println(sourceStrArray[i]); } // Split out up to 3 strings int maxSplit = 3; sourceStrArray = sourceStr.split(",", maxSplit); for (int i = 0; i < sourceStrArray.length; i++) { System.out.println(sourceStrArray[i]); } }} Output result:
12345123,4,5
The split implementation directly calls the split method of the matcher class. When using the String.split method to delimit strings, if the delimiter uses some special characters, it may not get the results we expected.
Characters with special meanings in regular expressions must be escaped when we use them. Example:
public class StringSplit { public static void main(String[] args) { String value = "192.168.128.33"; // Note that you should add //, either do not come out, yeah String[] names = value.split("//."); for (int i = 0; i < names.length; i++) { System.out.println(names[i]); } }}Summary of split separator
1. The characters "|", "*", "+" must be added with escaped characters, and "//" is added before them.
2. If it is "/", then it must be written as "////".
3. If there are multiple delimiters in a string, you can use "|" as a hyphen.
For example: String str = "Java string-split#test" , you can use Str.split(" |-|#") to separate each string. This divides the string into 3 substrings.
2. java.util.Tokenizer JDK 1.0 or later
StringTokenizer
The StringTokenizer class allows applications to break down strings into tokens. StringTokenizer is a legacy class that is preserved for compatibility reasons (although it is not encouraged in new code). It is recommended that all those seeking this feature use String's split method or java.util.regex package.
Code Example
public class StringSplit { public static void main(String[] args) { String ip = "192.168.128.33"; StringTokenizer token=new StringTokenizer(ip,"."); while(token.hasMoreElements()){ System.out.print(token.nextToken()+" "); } }}However, for the splitting of the string "192.168..33", the returned string array has only 3 elements, and the empty string between the two separators will be ignored. This should be used with caution.
However, String.split ( String.split is matched with regular expressions, so it does not use the KMP string matching algorithm) uses algorithms that traverse in sequence, with a high time complexity of O(m*n), so in terms of performance, StringTokenizer is much better. For applications that frequently use string segmentation, such as etl data processing, the performance of using StringTokenizer can be improved a lot.
Summarize
The above is the entire content of this article. I hope the content of this article will be of some help to your study or work. If you have any questions, you can leave a message to communicate.