StringTokenizer class in Java lets you split a String into multiple tokens.
In this tutorial, we will take a look at ways in which you can make use of this class to split your String.
Constructors:- StringTokenizerExample(String str)
- StringTokenizerExample(String str, String delim)
- StringTokenizer(String str, String delim, boolean returnDelims)
StringTokenizerExample(String str)
Example 1: Space-separated Stringimport java.util.StringTokenizer;
/**
* Example: StringTokenizer
* Author: Code2care.org
* Date: 03-Apr-2022
*
*/
public class StringTokenizerExample {
public static void main(String[] args) {
String myStr = "a b c d e f g";
StringTokenizer stringTokenizer = new StringTokenizer(myStr);
while (stringTokenizer.hasMoreTokens()) {
System.out.println(stringTokenizer.nextToken());
}
}
}
Output:
a
b
c
d
e
f
g
Example 2: Tab Separated String
String myStr = "1 2 3 4 5 6 7";
StringTokenizer stringTokenizer = new StringTokenizer(myStr);
while (stringTokenizer.hasMoreTokens()) {
System.out.println(stringTokenizer.nextToken());
}
Output:
1
2
3
4
5
6
7
Example 3: \r \n \t space all combiled:
String myStr = "Hello\nThere\rHow are you";
StringTokenizer stringTokenizer = new StringTokenizer(myStr);
while (stringTokenizer.hasMoreTokens()) {
System.out.println(stringTokenizer.nextToken());
}
Output:
Hello
There
How
are
you
Construtor Syntax:
public StringTokenizer(String str) {
this(str, " \t\n\r\f", false);
}
Note that the StringTokenizer methods do not distinguish between identifiers, numbers, and quoted strings, nor do they recognize and skip comments.
StringTokenizerExample(String str, String delim)
If you want to use a specific delimiter to tokenize your String, you can make use of the constructor with two arguments
public StringTokenizer(String str, String delim)
Example 4:import java.util.StringTokenizer;
/**
* Example: StringTokenizer with Delimiter
* Author: Code2care.org
* Date: 03-Apr-2022
*
*/
public class StringTokenizerExampleWithDelimiter {
public static void main(String[] args) {
String myStr = "1;2;2,2;3;4;5";
StringTokenizer stringTokenizer = new StringTokenizer(myStr,";");
while (stringTokenizer.hasMoreTokens()) {
System.out.println(stringTokenizer.nextToken());
}
}
}
Output:
1
2
2,2
3
4
5
StringTokenizer(String str, String delim, boolean returnDelims)
If you also want to print the delimiter out you can make use of the this constructor,
Example 5:String myStr = "1;2;2,2;3;4;5";
StringTokenizer stringTokenizer = new StringTokenizer(myStr,";",true);
while (stringTokenizer.hasMoreTokens()) {
System.out.println(stringTokenizer.nextToken());
}
Output:
1
;
2
;
2,2
;
3
;
4
;
5
More Posts related to Java,
- Get the current timestamp in Java
- Java Stream with Multiple Filters Example
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Fix: UnsupportedClassVersionError: Unsupported major.minor version 63.0
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- 7 deadly java.lang.OutOfMemoryError in Java Programming
- How to Calculate the SHA Hash Value of a File in Java
- Java JDBC Connection with Database using SSL (https) URL
- How to Add/Subtract Days to the Current Date in Java
- Create Nested Directories using Java Code
- Spring Boot: JDBCTemplate BatchUpdate Update Query Example
- What is CA FE BA BE 00 00 00 3D in Java Class Bytecode
- Save Java Object as JSON file using Jackson Library
- Adding Custom ASCII Text Banner in Spring Boot Application
- [Fix] Java: Type argument cannot be of primitive type generics
- List of New Features in Java 11 (JEPs)
- Java: How to Add two Maps with example
- Java JDBC Transition Management using PreparedStatement Examples
- Understanding and Handling NullPointerException in Java: Tips and Tricks for Effective Debugging
- Steps of working with Stored Procedures using JDBCTemplate Spring Boot
- Java 8 java.util.Function and BiFunction Examples
- The Motivation Behind Generics in Java Programming
- Get Current Local Date and Time using Java 8 DateTime API
- Java: Convert Char to ASCII
- Deep Dive: Why avoid java.util.Date and Calendar Classes
More Posts:
- Go to Line Number option in Windows Notepad - NotepadPlusPlus
- [JEP 430] Java JDK 21 New Feature - String Templates (Preview) - Java-JDK-21
- 29: Program to move a file in Python - Python
- Handling NullPointerException with Java Predicate - Java
- Calculate Volume of Cone - C-Program
- Android Emulator 5.1.1 not loading on Mac OS X Android Studio - Android-Studio
- How to change the System Settings Sidebar icon size Mac Ventura 13 - MacOS
- How to Sync iPhone with iPad - iOS