- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java StringTokenizer and String Split Example.
The StringTokenizer class allows an application to break a string into tokens.
This class is a legacy class that is retained for compatibility reasons although its use is discouraged in new code.
Example
import java.util.*; public class Sample { public static void main(String[] args) { // creating string tokenizer StringTokenizer st = new StringTokenizer("Come to learn"); // counting tokens System.out.println("Total tokens : " + st.countTokens()); // checking tokens while (st.hasMoreTokens()) { System.out.println("Next token : " + st.nextToken()); } } }
Output
Total tokens : 3 Next token : Come Next token : to Next token : learn
The split() method of the String class splits this string around matches of the given regular expression.
Example
import java.io.*; public class Test { public static void main(String args[]) { String Str = new String("Welcome-to-Tutorialspoint.com"); System.out.println("Return Value :" ); for (String retval: Str.split("-")) { System.out.println(retval); } } }
Output
Return Value : Welcome to Tutorialspoint.com
- Related Articles
- How to split a Java String into tokens with StringTokenizer?
- Java String split() method example.
- StringTokenizer methods in Java
- StringTokenizer class in Java
- Split a string in Java
- Java program to split and join a string
- What is the significance of the StringTokenizer class of the Java? Explain with an example?
- Split String with Dot (.) in Java
- Split String with Comma (,) in Java
- Java String Contains Example.
- Constructors of StringTokenizer class in Java.
- Methods of StringTokenizer class in Java.
- How to split a string in Java?
- Java String compareTo() Method example.
- Java String substring() Method example.

Advertisements