How Should Delimiters Be Treated?
When handling delimiters in Java, we often need to:
- Split a string into an array - This is useful when processing CSV data or user inputs.
- Extract meaningful data - We may want to extract values from a formatted string.
- 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.