Open In App

How to Split a String in Java with Delimiter?

Last Updated : 04 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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

  • Using split() method of the String class
  • Using String Tokenizer class
  • Using Scanner class

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




// 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:

  • We declare a string variable str with the value ” Hello, Good, Morning”.
  • Now, we split the string using the split() method with delimiter “,”.
  • After this, we iterate through an array of string str2 and print each substring in a new line.

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




// 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:

  • Here, we have created a new string Tokenizer object named st with input string “Java Programming”.
  • Now, iterates through tokens using hasMoreToken() method to check if there are more tokens in the string.
  • Now, inside the loop str.nextToken() is used to get the next token and print each token to console.

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




// 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:

  • We declare a string variable str with the value “Geeks For Geeks”.
  • Now, we have created scanner class object named fromStr.
  • Here, we print a message showing that the words in the string will be displayed.
  • Then, we have used while to iterates through the tokens using the hasNext() method to check if there are more tokens.
  • At last, it prints each word to console fromStr.next() is used to get the next token based on delimiter.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads