StringTokenizer in Java
The StringTokenizer class from the java.util package enables applications to segment or partition a string into smaller components based on a specified delimiter (with space being the default delimiter). Each partition of the split string is referred to as a token. This feature is especially beneficial for text processing tasks, allowing the string to be divided into multiple segments, with each segment serving as an individual element for further processing.
StringTokenizer st = new StringTokenizer("Java String Tutorial");
From the javadocs: - StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
The following Java program split the given string with space as delimiter
Example
import java.util.*;
class TestClass{
public static void main (String[] args){
//string characters seperated by space
String str = "Java String Tutorial";
//space is the default delimiter
//so we dont specify any delimiter
StringTokenizer st = new StringTokenizer(str);
while (st.hasMoreElements()) {
System.out.println(st.nextElement());
}
}
}
Output
Java
String
Tutorial
StringTokenizer with coma(,) as delimiter
import java.util.*;
class TestClass{
public static void main (String[] args){
//string characters seperated by coma(,) delimiter
String str = "NORTH,SOUTH,EAST,WEST";
StringTokenizer st = new StringTokenizer(str,",");
while (st.hasMoreTokens()) {
System.out.println(st.nextElement());
}
}
}
Output
NORTH
SOUTH
EAST
WEST
How to read and parse CSV file
The following program read a CSV file and split the character with coma(,) delimiter
import java.util.*;
import java.io.*;
class TestClass
{
public static void main (String[] args) {
BufferedReader bReader = null;
try{
String line;
bReader = new BufferedReader(new FileReader("d:/sample.csv"));
while ((line = bReader.readLine()) != null) {
StringTokenizer st = new StringTokenizer(line,",");
while (st.hasMoreTokens()) {
System.out.println(st.nextElement());
}
}
}catch(IOException ex1){
ex1.printStackTrace();
}finally {
try {
if (bReader!= null)
bReader.close();
} catch (IOException ex2) {
ex2.printStackTrace();
}
}
}
}
Conclusion
Please note that StringTokenizer is a legacy class, and it is recommended to use String.split() or other more modern techniques for tokenization, especially when regular expressions are needed for more complex scenarios.