Examples of two methods for splitting strings in Java

  • 2020-05-24 05:39:46
  • OfStack

preface

As you probably all know in java programming, sometimes you need to segment a string by a particular character, letter, etc., so you can use part 1 of the string or save all of your intercepts into an array, etc. The following article shares two segmentation methods for you, let's take a look at the following.

1. java.lang.String split() method, JDK 1.4 or later

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

The 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 at most 3 A string 
  int maxSplit = 3;
  sourceStrArray = sourceStr.split(",", maxSplit);
  for (int i = 0; i < sourceStrArray.length; i++) {
   System.out.println(sourceStrArray[i]);
  }
 }
}

Output results:


1
2
3
4
5
1
2
3,4,5

The implementation of split directly calls the split methods of the matcher class. In the use of String.split When the string is delimited by the method, the delimiter may not get the result we expected if it USES some special characters of 1.

Characters with special meanings in regular expressions must be escaped when used. Examples:


public class StringSplit {
 public static void main(String[] args) {
  String value = "192.168.128.33";
  //  Pay attention to add \\, You can't get out ,yeah
  String[] names = value.split("\\.");
  for (int i = 0; i < names.length; i++) {
   System.out.println(names[i]);
  }
 }
}

Summary of the split separator

1. The characters "|","*","+" must be escaped, preceded by "\ ".

2. If it's "\", it should be "\\\ ".

3. If there are more than one delimiter in a string, use "|" as the hyphen.

Such as: String str = "Java string-split#test" , you can use Str.split(" |-|#") Separate each string. This splits the string into three substrings.

2. java.util.Tokenizer JDK 1.0 or later

StringTokenizer

The StringTokenizer class allows an application to decompose a string into tokens. StringTokenizer is a legacy class that is retained for compatibility reasons (although its use is discouraged in new code). It is recommended that anyone seeking this feature use the split method of String or the java.util.regex package.

Code sample


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()+" "); 
  }

 }
}

But StringTokenizer for the string "192.168.. A 33" split returns an array of strings with only three elements. Empty strings between two separators are ignored. Use this with caution.

but String.split ( String.split KMP string matching algorithm is used for regular expression matching, so KMP string matching algorithm is not used. All the algorithms are traversed in order, and the time complexity of O(m*n) is relatively high, so StringTokenizer is much better in performance. For the frequent use of string segmentation applications, such as etl data processing, StringTokenizer performance can be greatly improved.

conclusion

The above is the whole content of this article, I hope the content of this article to your study or work can bring 1 definite help, if you have questions you can leave a message to communicate.


Related articles: