Abbey Workshop | ||||||||||
Click on the following link to to see the sample code for splitting a string a part using the StringTokenizer class.
Download source for: SplitString.java
C:\java\examples>java SplitString
Token 1 is [one]
Token 2 is [two]
Token 3 is [three]
Token 4 is [four]
Token 5 is [five]
Token 1 is [item one]
Token 2 is [item two]
Token 3 is [item three]
Token 4 is [item four]
Line 12 shows the first string to be parsed. Notice there a number of spaces between a couple of the items. Line 13 splits up the string. By default, the delimiter is a space and any extra spaces are automatically removed. Lines 15 - 18 show all parsed elements are walked through using the nextElement() method.
Lines 22 and 23 show how an alternative delimiter is specified when the StringTokenizer object is specified.
Copyright © Abbey Workshop 2006