Table of contents
1.
Introduction
2.
What Are Delimiters?
3.
How Should Delimiters Be Treated?
4.
How Are Special Characters Treated as Delimiters in the split() Method?  
5.
Program to Split a String with Delimiter in Java
5.1.
Example: Splitting a String Using split()
6.
Other Ways to Split a String with Delimiter in Java
6.1.
Using StringTokenizer Class
6.1.1.
Example
6.2.
Using Scanner Class
6.2.1.
Example:
7.
Frequently Asked Questions
7.1.
What is the easiest way to split a string using a delimiter in Java?
7.2.
Can I use multiple delimiters in Java?
7.3.
What is the difference between split() and StringTokenizer?
8.
Conclusion
Last Updated: Mar 17, 2025
Easy

Java Scanner delimiter() Method

Author Sinki Kumari
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The Java Scanner delimiter() method is used to retrieve the current delimiter that the Scanner object is using to tokenize input. By default, a Scanner uses whitespace as the delimiter, but it can be changed using the useDelimiter() method. This method is useful when working with structured data formats such as CSV or logs. 

Java Scanner delimiter() Method

In this article, we will discuss the delimiter() method in Java with examples to demonstrate its usage.

What Are Delimiters?

Delimiters are symbols or characters that separate elements in a string. Some common delimiters include:

  • Comma (,)
     
  • Space ( )
     
  • Semicolon (;)
     
  • Colon (:)
     
  • Pipe (|)

For example, in the string "apple,banana,grapes", the comma (",") is the delimiter that separates the words.

How Should Delimiters Be Treated?

When handling delimiters in Java, we often need to:

  1. Split a string into an array - This is useful when processing CSV data or user inputs.
     
  2. Extract meaningful data - We may want to extract values from a formatted string.
     
  3. Ignore unnecessary characters - Some delimiters need to be removed while processing data.

How Are Special Characters Treated as Delimiters in the split() Method?  

The `split()` method in Java is used to divide a string into an array of substrings based on a specified delimiter. Delimiters can be simple characters like commas or spaces, but they can also be special characters like `|`, `.`, ``, `+`, or `?`. However, special characters need to be handled carefully because many of them have specific meanings in regular expressions (regex).  

For example, the pipe character `|` is a regex metacharacter that means "OR." If you try to use it directly as a delimiter, the `split()` method won’t work as expected. To treat special characters as literal delimiters, you need to escape them using a backslash (`\`).  

Let’s look at a complete example to understand this better:  

public class DelimiterExample {
    public static void main(String[] args) {
        // Example string with a pipe (|) as the delimiter
        String data = "apple|banana|cherry|date";


        // Splitting the string using the pipe character as a delimiter
        // Note: The pipe character is escaped with double backslashes (\\) in Java
        String[] fruits = data.split("\\|");


        // Printing the result
        for (String fruit : fruits) {
            System.out.println(fruit);
        }
    }
} 
You can also try this code with Online Java Compiler
Run Code


In this Code:  

1. We have a string `data` that contains fruit names separated by the pipe character `|`.  
 

2. To split the string using `|` as the delimiter, we use `split("\\|")`. The double backslashes (`\\`) are necessary because the backslash itself is an escape character in Java.  
 

3. The `split()` method divides the string into an array of substrings.  
 

4. We then use a `for` loop to print each substring.  


Output:  

apple  
banana  
cherry  
date  


Important points to Remember:  

  • Special characters like `|`, `.`, ``, `+`, `?`, `^`, `$`, `(`, `)`, `[`, `]`, `{`, `}`, `\`, and `/` are regex metacharacters. If you want to use them as delimiters, you must escape them with a backslash (`\`).  
     
  • In Java, the backslash itself is an escape character, so you need to use double backslashes (`\\`) to escape special characters.  

Program to Split a String with Delimiter in Java

Java provides the split() method from the String class to divide a string based on a delimiter.

Example: Splitting a String Using split()

public class SplitExample {
    public static void main(String[] args) {
        String sentence = "Java,Python,C++,JavaScript";
        String[] languages = sentence.split(","); 
        for (String lang : languages) {
            System.out.println(lang);
        }
    }
}
You can also try this code with Online Java Compiler
Run Code

 

Output:

Java
Python
C++
JavaScript

 

In this program, the split() method breaks the string using "," as the delimiter.

Other Ways to Split a String with Delimiter in Java

Besides the split() method, Java provides additional ways to handle delimiters.

Using StringTokenizer Class

The StringTokenizer class is used to break a string into tokens based on a given delimiter.

Example

import java.util.StringTokenizer;
public class TokenizerExample {
    public static void main(String[] args) {
        String sentence = "apple|banana|grapes|orange";
        StringTokenizer tokenizer = new StringTokenizer(sentence, "|");  
        while (tokenizer.hasMoreTokens()) {
            System.out.println(tokenizer.nextToken());
        }
    }
}
You can also try this code with Online Java Compiler
Run Code


Output:

apple
banana
grapes
orange

 

Here, StringTokenizer separates words based on the pipe (|) delimiter.

Using Scanner Class

The Scanner class allows us to specify delimiters using useDelimiter() method.

Example:

import java.util.Scanner;
public class ScannerExample {
    public static void main(String[] args) {
        String data = "car-bike-truck-bus";
        Scanner scanner = new Scanner(data);
        scanner.useDelimiter("-");
        while (scanner.hasNext()) {
            System.out.println(scanner.next());
        }
        scanner.close();
    }
}

 

Output

car
bike
truck
bus

 

Here, Scanner reads tokens separated by "-" and prints them individually.

Frequently Asked Questions

What is the easiest way to split a string using a delimiter in Java?

The easiest way is to use the split() method of the String class.

Can I use multiple delimiters in Java?

Yes, you can use regular expressions in split() or configure Scanner to use multiple delimiters.

What is the difference between split() and StringTokenizer?

split() uses regular expressions, while StringTokenizer works with fixed delimiters and is less flexible.

Conclusion

The delimiter() method in Java's Scanner class returns the current pattern used for token separation. By default, Scanner uses whitespace as the delimiter, but it can be changed using the useDelimiter() method. This feature is useful when reading input with specific separators, such as commas or custom symbols. Using delimiter(), developers can verify or debug the current delimiter setting while processing input efficiently.

Live masterclass