Legacy classes and interfaces are the classes and interfaces that formed the Collection Framework in the earlier versions of Java and how now been restructured or re-engineered. Splitting of String is basically breaking the string around matches of the given regular expression.
Strings can be split in many ways in java but the 2 most common ways are using :
- StringTokenizer()
- split() method
The split() method is preferred and recommended even though it is comparatively slower than StringTokenizer.This is because it is more robust and easier to use than StringTokenizer.
A token is returned by taking a substring of the string that was used to create the StringTokenizer object. A StringTokenizer object internally maintains a current position within the string to be tokenized.
It has 3 constructors :
- StringTokenizer(String str)
- StringTokenizer(String str, String delimiter)
- StringTokenizer(String str, String delim, boolean flag)
Here,
- str: string to tokenized
- delimiter:delimiters to tokenize string(+,/ etc)
- flag: decides whether to consider delimiter as tokens(True/False)
Java
// Java program to demonstrate working of StringTokenizer() import java.util.*; class GFG { public static void main(String[] args) { String str = "This is geek" ; StringTokenizer st = new StringTokenizer(str, " " ); // counting tokens System.out.println( "Total tokens : " + st.countTokens()); // checking tokens for ( int i = 0 ; st.hasMoreTokens(); i++) System.out.println( "#" + i + ": " + st.nextToken()); } } |
Total tokens : 3 #0: This #1: is #2: geek
2. Split() String method
The string split() method breaks a given string around matches of the given regular expression.
There are 2 variants of the split() method in Java:
- String class method
public String [ ] split ( String regex, int limit ) Here, split(): method to split stri regex:a delimiting regular expression limit:the result threshold
- Using java.util.regex package
public String[] split(String regex) Here, split(): method to split string regex:a delimiting regular expression limit: default is 0
Java
// Java program to demonstrate split() import java.util.*; class GFG { public static void main(String[] args) { String str = " This is geek" ; String[] split = str.split( " " ); for ( int i = 0 ; i < split.length; i++) System.out.println( "#" + i + ": " + split[i]); } } |
#0: #1: This #2: #3: is #4: #5: geek
Difference Between StringTokenizer and Split Method in Java
StringTokenizer | Split() |
---|---|
It is a legacy class that allows an application to break a string into tokens. | It is a method of the String class or the java.util.regex package that splits this string around matches of the given regular expression. |
It returns one substring at a time. | It returns an array of substrings. |
It can’t handle empty strings well. | It can handle empty strings when you need to parse empty tokens like ant, bat, pat |
It is comparatively less robust & syntactically fussy. | It is more robust & has an easy syntax. |
It just accepts a String by which it will split the string | It accepts regular expressions. |
The delimiter is just a character long. | The delimiter is a regular expression. |
It is essentially designed for pulling out tokens delimited by fixed substrings. | It is essentially designed to parse text data from a source outside your program, like from a file, or from the user. |
Because of this restriction, it’s about twice as fast as split(). | Slower than StringTokeniser |
Consists of a constructor with a parameter that allows you to specify possible delimiter characters. | No constructor. |