Open In App

How to Split a String in Java with Delimiter?

In Java, the split() method of String class is used to break a string into an array of substrings based on a specified delimiter. We can split a string by character or split the string by words. The best example of this is CSV (Comma Separated Values) files.

Approaches to Split a String with Delimiter

Program to Split a String with Delimiter in Java

Below are the implementations of the three approaches to split a string.



Approach 1: Using split() method in String class

The split() method is used to break a large string into part based on a specified delimiter.




// Java program to split a string with
// Delimeter using split() method
import java.io.*;
  
// Driver Class
public class SplitExample 
{
      // Main Method
    public static void main(String args[])
    {
      // Declare a String 
      String str = " Hello, Good, Morning";
        
      // Split the string with comma delimiter
      String[] str1 = str.split(",");
        
      // Iterate through string
      for(String str2 : str1) 
      {
        // Printing the string
        System.out.println(str2);
    }
  }
}

Output

 Hello
 Good
 Morning

Explanation of the above Program:

Approach 2: Using String Tokenizer class

The String Tokenizer class in Java is present in java.util package. The main purpose of String Tokenizer is to divide or break the given strings into several tokens based upon the delimiter.




// Java program to split a string with
// Delimeter using String Tokenizer Class
import java.util.StringTokenizer;
  
// Driver Class
public class StringTokenizerExample 
{
      // Main Method
    public static void main(String[] args)
    {
     // Creating a new string tokenizer
     // Object  with given input string
     StringTokenizer st  = new StringTokenizer("Java Programming");
              
     // Print the input string
     System.out.println("Number of tokens are :" +st.countTokens());
                           
     // Iterate through token and print each token
     while (st.hasMoreTokens())
        {
           // Get next token and print it with space
           System.out.println("Token is : " +st.nextToken());
        }
    }
}

Output
Number of tokens are :2
Token is : Java
Token is : Programming

Explanation of the above Program:

Approach 3: Using Scanner Class

The Scanner class in Java allow a user to read input from the keyboard. Scanner class is used to tokenize an input string. Java Scanner class is present in java.util package.




// Java program to split a string with
// Delimeter using String Scanner Class
import java.util.Scanner;
  
// Driver Class
public class Test 
{
      // Main Function
    public static void main(String[] args)  
    {
      String str = "Geeks For Geeks";
        
      // Create a Scanner object with input string
      Scanner fromStr = new Scanner(str);
        
      // Print message indicating the
      // Words in the string will be shown.
      System.out.println("Words in the string are:  ");
        
      // Iterate through token and print each token
      while(fromStr.hasNext())
      {
        // Print the each string in new line
        System.out.println(fromStr.next());
      }
    }
}

Output
Words in the string are:  
Geeks
For
Geeks

Explanation of the above Program:


Article Tags :