By Alvin Alexander. Last updated: October 15, 2016
Can you show me a Java StringTokenizer example?
Sure. The Java StringTokenizer
class can be used to split a Java String
into tokens. Here's a simple StringTokenizer
example:
// start with a String of space-separated words String tags = "pizza pepperoni food cheese"; // convert each tag to a token StringTokenizer st = new StringTokenizer(tags," "); while ( st.hasMoreTokens() ) { String token = (String)st.nextToken(); System.out.println(token); }
This segment of code will have this output:
pizza pepperoni food cheese
Note that the StringTokenizer
can split a String
up using other characters besides a blank space; I just showed that one here for simplicity.