AS3 StringTokenizer

Thursday, November 1st, 2007

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:

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.

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:

An IIterator implementation would be as follows:

An IEnumeration implementation would be as follows

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

{ 10 comments to read ... please submit one more! }

  1. Cool, nice work Eric!
    Cheers, Thomas

  2. 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

  3. 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.

  4. Nice article and explanation, helped me lot to understand the full concept and very helpful explanation.

    Regards

    Philip

  5. 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.

  6. Very useful.
    I’ve appreciate.

    Regards

    Adrian

  7. I think you meant IIterator and not Iterator in the source code of StringTokenizer. Because the class signature reads that you are implementing the Iterator but there is no Iterator in the source code. Please let me know if my observation is true.

  8. Yes, you are correct. It’s a typo in the comment. Thanks for pointing it out.

    Eric

  9. Hey Eric,
    just wanted to say thank you for the classes and let you know that I will be using them in a project of mine.

    Thank you once again.

{ 1 Pingbacks/Trackbacks }

  1. Actionscript Classes » StringTokenizer