AS3 StringTokenizer
One useful utility in the java.util package is the StringTokenizer class. I was looking for an ActionScript implementation the other day but was unable to locate one. So after determining the level of effort to write a StringTokenizer in ActionScript was minimal, I decided to roll my own.
The ActionScript StringTokenizer is a convenience class which provides a simple mechanism from which Strings can be extracted into individual tokens based on a specific delimiter.
StringTokenizer provides an IIterator implementation as well as an IEnumeration implementation, thus allowing a StringTokenizer to be implemented as an IIterator or IEnumeration. The interfaces are also provided with the StringTokenizer API.
Ideally a StringTokenizer is to be utilized to tokenize a String into specific tokens and is to be considered read-only. To use StringTokenizer is very simple. The constructor accepts two arguments, both of which are required. The first argument is the source String from which tokens are to be extracted. The second argument is the delimiter from which the tokens are to be based on, as in the following:
var st:StringTokenizer = new StringTokenizer("R.I.A",".");
In addition to the constructor I have provided two static factory methods for creating both the IIterator implementation and IEnumeration implementation. The factory methods require the same arguments as the constructor and are provided as an alternative to the constructor.
var it:IIterator;
it = StringTokenizer.createIterator("R.I.A",".");
// or
var enum:IEnumeration;
enum = StringTokenizer.createEnumeration("R.I.A",".");
All implementations provide operations for determining if elements (tokens) remain as well as operations for retrieving the next element.
A typical StringTokenizer implementations is as follows:
var st:StringTokenizer = new StringTokenizer("R.I.A",".");
while ( st.hasMoreTokens() )
{
trace (st.nextToken() );
}
// outputs
// R
// I
// A
An IIterator implementation would be as follows:
var it:IIterator;
it = StringTokenizer.createIterator("R.I.A",".");
while ( it.hasNext() )
{
trace (it.next() );
}
// outputs
// R
// I
// A
An IEnumeration implementation would be as follows
var enum:IEnumeration;
enum = StringTokenizer.createEnumeration("R.I.A",".");
while ( enum.hasMoreElements() )
{
trace (enum.nextElement() );
}
// outputs
// R
// I
// A
In each use-case the StringTokenizer instance will return the same result as the underlying StringTokenizer implementation (e.g. invoking IIterator.next() results in a call to StringTokenizer.nextToken(), etc).
Source:
StringTokenizer
IEnumeration
IIterator
November 2nd, 2007 at 12:03 am
Cool, nice work Eric!
Cheers, Thomas
November 13th, 2007 at 5:43 pm
Is this any different than using the split function?
AS2:
var my_str:String = “P,A,T,S,Y”;
var my_array:Array = my_str.split(”,”);
for (var i = 0; i
November 13th, 2007 at 7:16 pm
The StringTokenizer API provides both an Enumeration and Iterator implementation which allows tokens to be accessed consistently, thus eliminating the need to manually, and redundantly provide a mechanism for retrieving tokens.
December 1st, 2007 at 5:25 pm
[…] http://www.ericfeminella.com/blog/2007/11/01/as3-stringtokenizer/ […]
March 8th, 2008 at 3:52 am
Nice article and explanation, helped me lot to understand the full concept and very helpful explanation.
Regards
Philip
April 17th, 2008 at 6:33 am
I have to agree with djmci. This is a fancy wrapper for the split function, which transforms it into a familiar Java idiom. No doubt Java developers programming Actionscript will love it though.
July 20th, 2008 at 10:26 am
Very useful.
I’ve appreciate.
Regards
Adrian