TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to JAVA

Java StringTokenizer Example

Understand the StringTokenizer class, which should be avoided. Split should instead be used.
StringTokenizer. Strings often contain many parts. With StringTokenizer we can separate these parts based on a delimiter. This class gets "tokens" from Strings.Strings
It is obsolete. The StringTokenizer class should be avoided. Instead, using split() with a regular expression is a better approach. StringTokenizer exists for compatibility.
An example program. We create a StringTokenizer with an input String containing three substrings separated by commas or spaces. With StringTokenizer we split its tokens apart.

HasMoreTokens: This returns true if the StringTokenizer contains more tokens. The delimiter is known from the last call to nextToken.

NextToken: This receives a String. Any of the characters in the string are considered possible delimiters. It returns the next token.

While: This loop will continue iterating infinitely, until the hasMoreTokens() method returns false.

While
Java program that uses StringTokenizer import java.util.StringTokenizer; public class Program { public static void main(String[] args) { // Create tokenizer with specified String. StringTokenizer tokenizer = new StringTokenizer("123,cat 456"); // Loop over all tokens separated by comma or space. while (tokenizer.hasMoreTokens()) { System.out.println(tokenizer.nextToken(", ")); } } } Output 123 cat 456
Use split. This is the recommended approach. We tokenize a string with the Split method. We can specify a character set, or use a non-word metacharacter like \s to indicate the separator.Split

Also: Regular expression patterns, and groups, may be used instead of split and the StringTokenizer class.

Regex
Java program that uses split public class Program { public static void main(String[] args) { final String value = "123,cat 456"; // Tokenize the String with a regular expression in Split. String[] tokens = value.split("[,\\ ]"); for (String token : tokens) { System.out.println(token); } } } Output 123 cat 456
Deprecated classes like StringTokenizer are not recommended in new programs. But knowing about them, and why they are not used, is helpful. It allows us to make better design decisions.
StringTokenizer is more confusing and verbose than split. In the split() program, fewer lines are used, fewer methods are called, and there is less syntax noise. Simpler is better.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf