Java StringTokenizer example to Split a String

Java StringTokenizer to Split a String Example :

Using StringTokenizer class, we can split a string into tokens.We can specify the delimiter that is used to split the string. For example, ‘Hello World’ string can be split into ‘Hello’ and ‘World’ if we mention the delimiter as space (”). In this tutorial, we will learn how to use ‘StringTokenizer’ to split a string. I will show you two different examples: to split a string by space and to split by a character (e.g. $).

StringTokenizer to split a string by space :

import java.util.Scanner;
import java.util.StringTokenizer;

public class Main {

    /**
     * Utility functions for System.out.println() and System.out.print()
     */
    private static void print(String str) {
        System.out.print(str);
    }

    private static void println(String str) {
        System.out.println(str);
    }

    public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);

        String userInputString;

        println("Please enter a string with multiple words : ");
        userInputString = scanner.nextLine();
        
        StringTokenizer tokenizer = new StringTokenizer(userInputString, " ");

        while(tokenizer.hasMoreTokens())
        {
            print(tokenizer.countTokens() + " words remaining to print . Current word : ");
            println(tokenizer.nextToken());
        }

    }
}

Sample Output :

Please enter a string with multiple words : 
This is a line separated by space
7 words remaining to print . Current word : This
6 words remaining to print . Current word : is
5 words remaining to print . Current word : a
4 words remaining to print . Current word : line
3 words remaining to print . Current word : separated
2 words remaining to print . Current word : by
1 words remaining to print . Current word : space

Explanation :

  1. First take the string to a variable ‘userInputString’ using ‘.nextLine()’ method of ‘Scanner’ class

  2. Create one ‘StringTokenizer’ object ‘tokenizer’. While creating the object, we are passing the ‘string’ and delimiter to separate the string in the constructor.

  3. Run one while loop. ‘.hasMoreTokens()’ returns true if more tokens available.

  4. ‘.nextToken()’ is used to print the token. And ‘.countToken()’ is used to get the number of tokens available.

Similar to this example, we can also split a string by any other character. Following example demonstrates how to split by ’$’ :

Example to split a string by ’$’ using StringTokenizer :

import java.util.Scanner;
import java.util.StringTokenizer;

public class Main {

    /**
     * Utility functions for System.out.println() and System.out.print()
     */
    private static void print(String str) {
        System.out.print(str);
    }

    private static void println(String str) {
        System.out.println(str);
    }

    public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);

        String userInputString;

        println("Please enter a string with multiple words : ");
        userInputString = scanner.nextLine();

        StringTokenizer tokenizer = new StringTokenizer(userInputString, "$");

        while(tokenizer.hasMoreTokens())
        {
            print(tokenizer.countTokens() + " words remaining to print . Current word : ");
            println(tokenizer.nextToken());
        }

    }
}

Sample Output :

Please enter a string with multiple words : 
This$is$a$string
4 words remaining to print . Current word : This
3 words remaining to print . Current word : is
2 words remaining to print . Current word : a
1 words remaining to print . Current word : string

Everything is same as the first one. Here we are passing ’$’ as the delimiter. That’s it. It splits the string by ’$‘.

Similar tutorials :