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,
- How to install Java 11 on Mac
- Get Client IP address from HTTP Response in Java
- SharePoint Open in the client application document opens in browser
- How to verify if java is installed on the computer and get version detail
- Java - Check if array contains the value
- Your JBoss Application Server 7 is running However you have not yet added any users to be able to access the admin console
- How to run Java Unit Test cases with Apache Maven?
- What Java version is used for Minecraft 1.18
- List of jar files for Jax-ws (SOAP) based Java Web Services
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- Convert Instant timestamp into LocalDateTime Java Code Example
- Java - Calculate time taken for the code to execute in milliseconds or nanoseconds
- Create simple struts2 project using maven commands
- Java - PatternSyntaxException
- List of Online Java compiler with console
- Minecraft Java Edition
- How to declare and initialize Array in Java Programming
- [Solved] com.sun.xml.ws.transport.http.servlet.WSServletContextListener ClassNotFoundException
- Java XML-RPC 3.1.x based web service example
- Simple Struts 2 Tutorial in eclipse with tomcat 7 server
- Java 8 foreach loop code examples
- Java -Day of the week using Java 8 DayOfWeek Enum
- Java 8 - Convert List to Map Examples
- Java: TimeZone List with GMT/UTC Offset
- list of jars required for hibernate 4.x.x
More Posts:
- SharePoint Server 2016 Preview installation error - This Product Key isn't a valid Microsoft Office 2016 Product Key. Check that you've entered it correctly. - SharePoint
- How to find AUTO_INCREMENT Fields value in MySQL table - MySQL
- Send Email with attachment using SharePoint PowerShell, SMTP server - SharePoint
- Eclipse Error : The Eclipse executable launcher was unable to locate its companion shared library. - Eclipse
- Failed to load the JNI shared library jvm.dll - Eclipse
- JSON Text to JavaScript Object using eval() Example: JSON Tutorial - Json-Tutorial
- Python Sleep Function/Method Code Example - Python
- Ho to upgrade Docker Desktop on Mac - MacOS