java TokenTester "val = 12+8" val = 12 + 8
Notice that the spaces are returned as well as the other delimiters and tokens. This might not be quite what you want. You might like to ignore spaces completely and use only "=+-" as delimiters. The following does that:
import java.util.* ;
public class TokenTester
{
public static void main ( String[] args )
{
StringTokenizer tok;
if ( args.length > 0 )
{
tok = new StringTokenizer( args[0], "=+-", true ); // no space
while ( tok.hasMoreTokens() )
System.out.println( tok.nextToken().trim() );
}
}
}
Now nextToken()
returns the strings
"val ", "=", " 12", "+", and "8".
The trim()
method trims spaces off both ends
of these strings.