×

Difference between String Tokenizer and split Method in Java

Introduction

Today, let us understand the difference between String Tokenizer and Split Method. First, let us learn about the String Tokenizer and Split method individually and then know about their differences

String Tokenizer

The String Tokenizer is an in-built class present in java. util package. The in-built class name is StringTokenizer. This class is used to break a given String into small parts. These small parts are known as Tokens.

The given String is broken into Tokens only with a certain recurring condition. Generally, the String is a group of characters. These groups of characters are separated by a certain character

Example

1.) He is playing cricket
The above string has been divided by a special character known as space ( )
2.) A=1; b=1; v=1; d=1;
The above statement is also a string but is been divided with the help of a special character semi colon ( ; )

These special characters used for dividing the String into parts are known as delimiters.

Here, the delimiter of the first statement is space and the delimiter of the second statement is a semi colon.

Methods in String Tokenizer

MethodUse
countTokens ( )The countTokens ( ) method is used to know the total number of tokens created from the specified String. The return data type for this method is Integer.
hasMoreTokens ( )The hasMoreTokens ( ) method is used to know whether we have any tokens further or not. If present, the value is true and in other cases false. The return data type for this method is Boolean.  
hasMoreElements ( )The method hasMoreElements ( ) works same as the method hasMoreTokens ( )
nextToken ( )The nextToken ( ) method is used to get the value of token. The values are printed only after checking the condition with the condition of hasMoreTokens ( ) The return data type for this method is String
nextElement ( )The method nextElement ( ) works same as the method nextToken ( ) The return data type for this method is the data type of the object created, except that its declared return value is Object rather than String

StringTokenizer Class Initialization

Syntax

1.) StringTokenizer object name = new StringTokenizer (String s1);
Here, the delimiters are taken directly as Space.
2.) StringTokenizer object name = new StringTokenizer (String s1, String delimiter1);
Here, the delimiters are specified by us.

Example Program

File Name: StrTok. java

// A program for String Tokenizer
import java. util. *;


public class StrTok
{
public static void main (String [ ] args) 
{

StringTokenizer st1 = new StringTokenizer ("He is Ben Stokes");
// No Delimiter Specified
System. out. println ("The total number of Tokens in String Tokenizer 1 are: "+st1. countTokens ());
System. out. println ("The token value of String Tokenizer 1 is: ");
while (st1. hasMoreTokens ())
{
    System. out. println (st1. nextToken ());
}

        System. out. println ("_  _  _  _  _  _  _   _  _  _  _  _  _  _  _  _\n");


StringTokenizer st2 = new StringTokenizer ("Vikram, Amar, Rolex, Dilli",", ");
// A delimiter of comma and space is given ", "
System. out. println ("The total number of Tokens in String Tokenizer 2 are: "+st2. countTokens ());
System. out. println ("The token value of String Tokenizer 2 is: ");


while (st2. hasMoreTokens ())
{
    System. out. println (st2. nextToken ());
}
System. out. println ("_  _  _  _  _  _  _   _  _  _  _  _  _  _  _  _\n");


StringTokenizer st3 = new StringTokenizer ("Program finished with exit code 0");
// No Delimiter Specified
System. out. println ("The total number of Tokens in String Tokenizer 3 are: "+st3. countTokens ());
System. out. println ("The token value of String Tokenizer 4 is: ");


while (st3. hasMoreElements ())
{
    System. out. println (st3. nextElement ());
}
    System. out. println ("_  _  _  _  _  _  _   _  _  _  _  _  _  _  _  _\n");



StringTokenizer st4 = new StringTokenizer ("Brand_ value_ is_ equal_ to_ perception" ,"_ ");

// Delimiter "_ " is specified
System. out. println ("The total number of Tokens in String Tokenizer 4 are: "+st4. countTokens ());
System. out. println ("The token value of String Tokenizer 4 is: ");


while (st4. hasMoreElements ())
{
    System. out. println (st4. nextElement ());
}
System. out. println ("_  _  _  _  _  _  _   _  _  _  _  _  _  _  _  _\n");





}
}

Output

The total number of Tokens in String Tokenizer 1 are: 4
The token value of String Tokenizer 1 is: 
He
is
Ben
Stokes
_  _  _  _  _  _  _   _  _  _  _  _  _  _  _  _


The total number of Tokens in String Tokenizer 2 are: 4
The token value of String Tokenizer 2 is: 
Vikram
Amar
Rolex
Dilli
_  _  _  _  _  _  _   _  _  _  _  _  _  _  _  _


The total number of Tokens in String Tokenizer 3 are: 6
The token value of String Tokenizer 4 is: 
Program
finished
with
exit
code
0
_  _  _  _  _  _  _   _  _  _  _  _  _  _  _  _


The total number of Tokens in String Tokenizer 4 are: 6
The token value of String Tokenizer 4 is: 
Brand
value
is
equal
to
perception
_  _  _  _  _  _  _   _  _  _  _  _  _  _  _  _

Split Method in Java

This is a method present in the java package named java. lang. This method is used to divide the elements into different entities. This is done for better viewing. The split method is an in-built method for Strings. So, this is used for better viewing of given or created String.

The elements after splitting are stored in String data typed array. The split method has two different syntaxes. Let us specify them first, and then explain them and their usage.

Syntax

1. String name. split ( String s1); // Here s1 indicates the delimiter not the String variable name
2. String name. split ( String s1, int limit);  // Here s1 indicates the delimiter not the String variable name

Let us now learn about the first syntax now

String Name. split (String s1) Usage

Here, s1 indicates the delimiters. All the delimiters are considered as a String. Usually, a String is divided into small parts with the help of some delimiters. We use split method which has specified delimiter divides the whole String into smaller Strings and we store them in separate String arrays.

Example Program

File Name: Split1. java

// A program for explaining the first syntax of split ( ) method
public class Split1
{
public static void main (String[ ] args) 
{
    String str = "Don't be afraid to give up the good to go for the great";
    
    String arr [ ] = str. split (" ");
    
        System. out. println ("The String after splitting is:");
    
    
    for (String i : arr)
    {
        System. out. println (i);
    }



}
}

Output

The String after splitting is:
Don't
be
afraid
to
give
up
the
good
to
go
for
the
great

Explanation

The given String is - > Don't be afraid to give up the good to go for the great

The words in String are separated from each other by a delimiter named space ( “ “ )

The split method uses this delimiter and stores the small sub String which occurs before the occurrence of delimiter into the created String Array.

Example: This is Page

Here, the space occurs immediately after This. So, This is stored into the created String Array.

Example Program 2

File Name: Split2. java

// A program for explaining the first syntax of split ( ) method
public class Split2
{
public static void main(String[] args) 
{
    String str = "Don't_ be_ afraid_ to_ give_ up_ the_ good_ to_ go_ for_ the_ great";
    
    String arr [] = str. split ("_ ");
    
        System. out. println ("The String after splitting is:");
    
    
    for (String i : arr)
    {
        System. out. println (i);
    }



}
}

Output

The String after splitting is:
Don't
be
afraid
to
give
up
the
good
to
go
for
the
great

String Name. split (String s1, int limit) Usage

Here, s1 indicates the delimiters. All the delimiters are considered as a String. Usually, a String is divided into small parts with the help of some delimiters. We use the split method which has a specified delimiter that divides the whole String into smaller Strings and we store them in separate String arrays.

All the splitting goes in the same way. But the number of splits occurring depends upon the value of limit. Based on the value of the limit, the number of sub strings created is dependent.

To be precise, the number of sub Strings created is numerically equal to the value of the limit specified.

Example Program

File Name: Split3. java

// A program for explaining the second syntax of split ( ) method
public class Split3
{
public static void main(String[] args) 
{
    String str = "Don't be afraid to give up the good to go for the great";
    
    String arr [] = str. split (" ", 7);
    
        System. out. println ("The String after splitting is:");
    
    
    for (String i : arr)
    {
        System. out. println (i);
    }



}
}

Output

The String after splitting is:
Don't
be
afraid
to
give
up
the good to go for the great

Explanation

The given String is - > Don't be afraid to give up the good to go for the great

The words in String are separated from each other by a delimiter named space ( “ “ )

The split method uses this delimiter and stores the small sub String which occurs before the occurrence of delimiter into the created String Array.

The limit value limits the total number of sub Strings created.

Here the limit value is seven ( 7 )

So, splitting will be done till the sixth delimiter is found.

After that splitting process is stopped and remaining chunk of String becomes a last sub String of the created String Array.

Example Program 2

File Name: Split4. java

// A program for explaining the sec syntax of split ( ) method
public class Mainond
{
public static void main(String[] args) 
{
    String str = "Don't be afraid to give up the good to go for the great";
    
    String arr [] = str. split (" ", 5);
    
   System. out. println ("The String after splitting is:");
      System. out. println ("The 5 SUB STRINGS are: ");


   
    
    
    for (String i : arr)
    {
        System. out. println (i);
    }



}
}

Output

The String after splitting is:
The 5 SUB STRINGS are: 
Don't
be
afraid
to
give up the good to go for the great

Differences between StringTokenizer and split Method

StringTokenizersplit ( ) method
It is a class present in java. util packageIt is a method present in java. lang package
It can only return a single Sub String at a timeThis method has the capability to return an array of Sub Strings at a single method call
It has constructorsNo constructors are available
The String Tokenizer have a few methods like countTokens ( ), hasMoreTokens ( ), nextToken ( ), hasMoreElements ( ), nextElement ( ) .split ( ) method itself is a method
The String Tokenizer do not have the concept of limiting the creation of Sub StringsThe split method has the concept of limiting the creation of Sub Strings
Hard Syntax to remember and not as flexible as spilt ( ) method for applicationEasy Syntax and can be applied easily
Less powerful when compared to the  split ( ) methodMore powerful when compared to the String Tokenizer concept
Very FastSlow when compared to the String Tokenizer
Empty Strings cannot be specifiedEmpty Strings can be specified

Related Topics

Design of JDBC

Java applications may interface using database systems from many vendors using the Java Database Connectivity (JDBC) Application Software Interface (API) from Sun Microsystem. To connect spreadsheets, JDBC and database drivers...

3 minutes read.

This Operator Using in Java

this keyword in Java has a wide range of applications. this reference variable in Programming language refers to the object of interest. This keyword is used in Java. this keyword is...

5 minutes read.

Java File

Java file class implements the concept of file handling. It has several methods, such as deleting, creating, reading, and updating files. This class allows java users to perform various operations...

5 minutes read.

Java LDAP Authentication

WHAT IS LDAP? Clients can communicate with directory services by sending requests and receiving responses using the Lightweight Directory Access Protocol (LDAP). The term "LDAP server" refers to a directory service...

7 minutes read.

Duodecimal in Java

Duodecimal is a notation style in which a number with a base of 12 is referred to be a duodecimal number. In Java, we can use to convert duodecimal integers...

2 minutes read.

The Maximum Rectangular Area in a Histogram in Java

Continuous bars should be used to form the largest possible rectangle. We'll assume in the interest of convenience that each bar's width is 1. Naive Approach In this method, each bar will be...

6 minutes read.

Swastika Pattern in Java

This part teaches us how to create the Swastika Pattern in Java utilizing user-defined columns and rows as well as stars or other special characters.A Java pattern application that is...

2 minutes read.

Java Base64 Encoding and Decoding

Introduction to Encoding and Decoding Encoding is the process of putting the sequence of characters like letters, numbers, punctuations, and other symbols into a specialised format for the efficient transmission or...

11 minutes read.

Java Swings

Swing is a Java Foundation Class library and an extension to the Abstract Window Toolkit (AWT) (JFC).As compared to AWT, Swing has significantly better functionality, new components, increased component features,...

9 minutes read.

Kotlin Vs Java

Kotlin Vs Java There are many languages available for Android development. Java is the official language for android development but Kotlin is becoming popular nowadays. This article discusses both of these...

4 minutes read.

Bin Packing Problem Java

Assigning some items with known weights to bins with consistent capacities is necessary for the bin packing problem. The goal is to use the smallest possible boxes while ensuring everything is put...

6 minutes read.

String Palindrome Program in Java

String Palindrome Program in Java The palindrome is a string, phrase, word, number, or other sequences of characters that can be read in both directions i.e. forward (left to right) and...

11 minutes read.

How to Run Applet Program in Java

An applet is a new type of program which is put in a webpage. By inserting applet into a webpage dynamic content can be created. Characteristics of an Applet The applet is...

11 minutes read.

Hybrid Inheritance in Java

The most crucial OOPs concept in Java is inheritance, which enables the transfer of a class's properties to another class. It describes the Is-A relationship generally. We can create a...

3 minutes read.

Balanced Prime Number in Java

This section will cover the definition of a balanced prime number as well as how to find one using a Java program. Balance Prime Number A prime number that is equivalent to...

5 minutes read.

Sleeping Barber Problem in Java

The barbershop in this issue has one barber, one barber chair, and N chairs for customers in the waiting area. We may demonstrate the issue by keeping with the original...

6 minutes read.

Java Open File

Java Desktop class give an open() strategy to open a filr. It has a place with a java.awt package. Work area execution is stage subordinate, so it is important to...

5 minutes read.

How to Print array in Java?

A Java array is a data structure that allows us to hold components of the same data type. An array's items are kept in a single memory region. As a...

6 minutes read.

Java 8 filters list

A stream with the components of this stream that match the given predicate is provided by the streaming filter (Predicate predicate). This process is step-by-step. Because these operations are always...

4 minutes read.

Tilt operator in Java | Tilde operator in Java Example

The symbol designates it as a unary operator (pronounced as the tilde). It gives back the bit's complement or inverse. Every 0 turns into a 1, and every 1 back...

3 minutes read.