In this post we will discuss on how to split a String using Java classes such as String, StringTokenizer and Scanner.
String.split(delimiter)
String.split(delimiter) splits the text at the given delimiter and creates an array of String objects.
String textToSplit = "1-John-Doe-35";
for (String token : textToSplit.split("-")) {
System.out.println(token);
}
Output
1
John
Doe
35
But when you want to split the text using pipe symbol (|), you need to escape that character, because pipe symbol is a meta character used in regex. So the code will be as follows:
textToSplit = "1|John|Doe|35";
for (String token : textToSplit.split("\\|")) {
System.out.println(token);
}
StringTokenizer(String)
- StringTokenizer’s constructor accepts a String that needs split
- Without delimiter, StringTokenizer breaks the String into tokens at word boundaries
- Unlike String.split(), StringTokenizer splits the text as needed
- Delimiters also can be specified per token basis, giving us the flexibility to change the delimiters as we proceed in the loop
- StringTokenizer.hasMoreTokens() returns boolean signifying if more tokens available or not
- StringTokenizer.nextToken() returns the token
textToSplit = "1 John Doe 35";
StringTokenizer tokenizer1 = new StringTokenizer(textToSplit);
while (tokenizer1.hasMoreTokens()) {
System.out.println(tokenizer1.nextToken());
}
StringTokenizer(String, delimiters)
- In this constructor variant StringTokenizer takes delimiters as the second parameter where you can pass one or more delimiters
textToSplit = "1|John|Doe|35";
StringTokenizer tokenizer2 = new StringTokenizer(textToSplit, "|");
while (tokenizer2.hasMoreTokens()) {
System.out.println(tokenizer2.nextToken());
}
Scanner(String).useDelimiter(delimiter)
- Scanner can parse a String using regex
- Scanner provides useDelimiter method that accepts delimiter and returns Scanner again
- Using Scanner.hasNext(), we can check for the availability of tokens
- Scanner.next() returns a token
textToSplit = "1|John|Doe|35";
Scanner scanner = new Scanner(textToSplit).useDelimiter("\\|");
while (scanner.hasNext()) {
System.out.println(scanner.next());
}
scanner.close();
Full Example
package com.techstackjournal;
import java.util.Scanner;
import java.util.StringTokenizer;
public class SplitString {
public static void main(String[] args) {
String textToSplit = "1-John-Doe-35";
System.out.println("String.split using hyphen:");
for (String token : textToSplit.split("-")) {
System.out.println(token);
}
System.out.println();
textToSplit = "1|John|Doe|35";
System.out.println("String.split using pipe:");
for (String token : textToSplit.split("\\|")) {
System.out.println(token);
}
System.out.println();
textToSplit = "1 John Doe 35";
System.out.println("Default StringTokenizer:");
StringTokenizer tokenizer1 = new StringTokenizer(textToSplit);
while (tokenizer1.hasMoreTokens()) {
System.out.println(tokenizer1.nextToken());
}
System.out.println();
textToSplit = "1|John|Doe|35";
System.out.println("StringTokenizer with delim:");
StringTokenizer tokenizer2 = new StringTokenizer(textToSplit, "|");
while (tokenizer2.hasMoreTokens()) {
System.out.println(tokenizer2.nextToken());
}
System.out.println();
textToSplit = "1|John|Doe|35";
Scanner scanner = new Scanner(textToSplit).useDelimiter("\\|");
System.out.println("Using Scanner:");
while (scanner.hasNext()) {
System.out.println(scanner.next());
}
scanner.close();
}
}
Output:
String.split using hyphen:
1
John
Doe
35
String.split using pipe:
1
John
Doe
35
Default StringTokenizer:
1
John
Doe
35
StringTokenizer with delim:
1
John
Doe
35
Using Scanner:
1
John
Doe
35