Java StringTokenizer – get tokens by index example shows how to get specific tokens using the index after the string content is tokenized using the StringTokenizer object.
How to get StringTokenizer tokens using the index?
The StringTokenizer class generates the tokens from the string content using the set of delimiters. The generated tokens then can be accessed using the nextToken
or the nextElement
method. Plus there are a couple of methods, namely hasMoreTokens
and hasMoreElements
, that let us know that whether there are more tokens left in the string content.
By combining these methods, we can iterate through the generated string tokens as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.javacodeexamples.stringtokenizerexamples; import java.util.StringTokenizer; public class StringTokenizerGetByIndex { public static void main(String[] args) { String strContent = "I love Java language"; //tokens will be generated by space StringTokenizer st = new StringTokenizer(strContent, " "); //iterate through all the tokens while( st.hasMoreTokens() ) { System.out.println( st.nextToken() ); } } } |
Output
1 2 3 4 |
I love Java language |
Except for these methods, the StringTokenizer class does not provide any methods that let us access the generated tokens using their index. However, we can do that by writing a custom method that returns a list of tokens or an array of tokens. Once we get the list or an array of tokens, we can access the specific tokens using the index.
Here is an example code for that.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
package com.javacodeexamples.stringtokenizerexamples; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; public class StringTokenizerGetByIndex { public static void main(String[] args) { String strContent = "I love Java language"; List<String> listToken = getTokenList(strContent, " "); //now we can access the tokens using index for(int i = 0; i < listToken.size() ; i++) { System.out.println("Index: " + i + ", value: " + listToken.get(i)); } } private static List<String> getTokenList(String string, String delimiters) { List<String> listTokens = new ArrayList<String>(); try { StringTokenizer st = new StringTokenizer(string, delimiters); while( st.hasMoreTokens() ) { //add token to the list listTokens.add( st.nextToken() ); } }catch(Exception e) { e.printStackTrace(); } return listTokens; } } |
Output
1 2 3 4 |
Index: 0, value: I Index: 1, value: love Index: 2, value: Java Index: 3, value: language |
Is this the recommended way to get string tokens by index?
NO. It is not. The StringTokenizer class is a legacy class that is retained only for backward compatibility purposes. You should use the String split method instead of the StringTokenizer whenever possible.
See below example where I have tokenized the same string using the split method. The split method returns an array of tokens, so we do not need to create any workarounds for the purpose of accessing the elements by index.
1 2 3 4 5 6 7 8 |
String strContent = "I love Java language"; //split String[] strTokens = strContent.split(" "); for(int i = 0; i < strTokens.length ; i++) { System.out.println("Index: " + i + ", value: " +strTokens[i]); } |
Output
1 2 3 4 |
Index: 0, value: I Index: 1, value: love Index: 2, value: Java Index: 3, value: language |
If you want to learn more about the tokenizer, please visit the Java StringTokenizer Tutorial.
Please let me know your views in the comments section below.