Java split string details and example code

  • 2020-05-26 08:34:55
  • OfStack

Java splits strings

split() method for java.lang.String, 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. When using the String.split method to separate strings, the separator may not get the result we expected if it USES 1 special characters. 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 with 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.

For example: String str = "Java string-split #test", you can use Str.split (" |-|#") to separate each string. This splits the string into three substrings.

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 all those seeking this functionality 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.

However, String.split (String.split USES regular expression matching, so KMP string matching algorithm is not used) USES the algorithm of traversal in order, and the time complexity of O(m*n) is relatively high. Therefore, StringTokenizer is much better in performance. For applications that frequently use string segmentation, such as etl data processing, StringTokenizer's performance can be greatly improved.

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: