BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

StringTokenizer vs Split Method – Which is better?

Last Updated: September 20, 2022 by Chaitanya Singh | Filed Under: java

In this article, you will learn the difference between StringTokenizer and split() method in Java.

Let’s see how these two methods are used to split a given string then we will discuss the difference.

StringTokenizer

import java.util.*;
class JavaExample {
  public static void main(String[] args)
  {
    String str = "20-09-2022";
    StringTokenizer strToken = new StringTokenizer(str, "-");

    // The returns the number of possible substrings after split
    System.out.println("Number of Substrings after split: "
            + strToken.countTokens());

    // printing the substrings after split
    for (int i = 0; strToken.hasMoreTokens(); i++)
      System.out.println((i+1)+": "+ strToken.nextToken());
  }
}

Output:
StringTokenizer vs split in Java
StringTokenizer(String str, String delimiter, boolean flag): You can also pass a boolean flag (true or false) while calling the StringTokenizer constructor.

Example 1: If the flag is false
Input : Given string is “Welcome readers” and delimiter is ” ”
code: StringTokenizer strToken = new StringTokenizer(str, " ", false);
Output: tokens are “Welcome” and “readers”. //two substrings

Example 2: If the flag is true
Input : Given string is “Welcome readers” and delimiter is ” ”
code: StringTokenizer strToken = new StringTokenizer(str, " ", true);
Output: tokens are “Welcome”, ” ” and “readers”. //three substrings

String split() Method

The split() method belongs to the java string class. This method is also used to split the method based on the provided delimiter.

class JavaExample {
  public static void main(String[] args)
  {
    String str = "Welcome to BeginnersBook";

    //delimiter is whitespace " " here
    String[] subStr = str.split(" ");

    System.out.println("Substrings after split: "+subStr.length);
    for (int i = 0; i < subStr.length; i++)
      System.out.println((i+1)+": " + subStr[i]);
  }
}

Output:
Split vs StringTokenizer

StringTokenizer vs Split Method – Which is better?

The split() method is the better than StringTokenizer, because it is more robust and easier to use. It is slower than StringTokenizer, however the StringTokenizer is legacy class so split() method is preferred method for string splitting.

Difference between StringTokenizer and split() method

StringTokenizer split() method
It is a class. It is a method of String class.
It returns a single substring at a time. The next substring is read using nextToken() method of this class. It returns an array of substrings. The substrings can be traversed just like a normal string array elements.
Syntax is complex. Syntax is easier.
StringTokenizer only accepts the string delimiter. The split() method can accept string delimiter and regular expressions.
It is faster because it doesn’t allow regular expression. It is comparatively slower but has more uses.
Substrings often referred as tokens are generated using the constructor of this class. It doesn’t have any constructors as it is not a class, rather a method.
Less Robust. More Robust.
❮ PreviousNext ❯

Top Related Articles:

  1. How to Split a String in Java with Delimiter
  2. Convert Comma Separated String to HashSet in Java
  3. Java String substring() Method with examples
  4. Split String by Newline in Java
  5. StringTokenizer in Java with Examples

Tags: Java-Strings

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap