Difference Between StringTokenizer and Split Method in Java Last Updated : 12 Dec, 2021 Comments Improve Suggest changes Like Article Like Report 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. 1. String Tokenizer 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 tokenizeddelimiter: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()); } } OutputTotal 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 methodpublic String [ ] split ( String regex, int limit ) Here, split(): method to split stri regex:a delimiting regular expression limit:the result thresholdUsing java.util.regex packagepublic 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]); } } Output#0: #1: This #2: #3: is #4: #5: geekDifference 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 stringIt 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 StringTokeniserConsists of a constructor with a parameter that allows you to specify possible delimiter characters.No constructor. Comment More infoAdvertise with us Next Article Difference Between StringTokenizer and Split Method in Java jelonmusk Follow Improve Article Tags : Java Technical Scripter Difference Between Technical Scripter 2020 Practice Tags : Java Similar Reads Difference Between charAt() and substring() Method in Java In Java, the charAt() method of the String class is used to extract the character from a string. It returns the character at the specified index in the String. The substring() method is used to extract some portion from the actual String and the actual string remains the same as it is. After that, t 3 min read Difference Between Iterator and Spliterator in Java The Java Iterator interface represents an object capable of iterating through a collection of Java objects, one object at a time. The Iterator interface is one of the oldest mechanisms in Java for iterating collections of objects (although not the oldest â Enumerator predated Iterator). Moreover, an 4 min read StringTokenizer countTokens() Method in Java with Examples The countTokens() method of StringTokenizer class calculate the number of times that this tokenizer's nextToken method can be called before the method generates any further exception. Note: The current position is not advanced during the process. Syntax: public int countTokens() Parameters: The meth 2 min read Difference between String and Character array in Java Unlike C/C++ Character arrays and Strings are two different things in Java. Both Character Arrays and Strings are a collection of characters but are different in terms of properties. Differences between Strings and Character Arrays:PropertyStringCharacter ArrayDefinitionA sequence of characters is r 3 min read StringTokenizer hasMoreElements() Method in Java with Examples The hasMoreElements() method of StringTokenizer class also checks whether there are any more tokens available with this StringTokenizer. It is similar to the hasMoreTokens(). The method exists exclusively so that the Enumeration interface of this class can be implemented. Syntax: public boolean hasM 2 min read How to split a string in C/C++, Python and Java? Splitting a string by some delimiter is a very common task. For example, we have a comma-separated list of items from a file and we want individual items in an array. Almost all programming languages, provide a function split a string by some delimiter. In C: // Splits str[] according to given delim 7 min read StringJoiner add() method in Java The add(CharSequence newElement) of StringJoiner adds a copy of the given CharSequence value as the next element of the StringJoiner value. If newElement is null, then "null" is added.Syntax: public StringJoiner add(CharSequence newElement) Parameters: This method takes a mandatory parameter newElem 1 min read StringTokenizer hasMoreTokens() Method in Java with Examples The hasMoreTokens() method of StringTokenizer class checks whether there are any more tokens available with this StringTokenizer. Syntax: public boolean hasMoreTokens() Parameters: The method does not take any parameters. Return Value: The method returns boolean True if the availability of at least 2 min read StringTokenizer nextElement() Method in Java with Examples The nextElement() method of StringTokenizer class is also used to return the next token one after another from this StringTokenizer. It is similar to the nextToken() method, except that the return type is Object rather than the String. Syntax: public Object nextElement() Parameters: The method does 2 min read Difference between split() and explode() functions for String manipulation in PHP In this article, we will see the differences between split() and explode() functions for String manipulation in PHP. The split() and explode() functions are available in base PHP and are used to perform string manipulations as well as conversions. split() Function: The split() function in PHP is use 3 min read Like