Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.

StringTokenizer Class

Visual Studio 2005

Provides methods to parse a string and split it into tokens based on a delimiter.

Package: java.util

Assembly: vjslib (in vjslib.dll)


public class java.util.StringTokenizer
    extends java.lang.Object
    implements java.util.Enumeration

The following example demonstrates the countTokens, hasMoreElements, hasMoreTokens, nextElement, and nextToken methods of the StringTokenizer class.

// stringtokenizer_overview.jsl

import java.util.*;

public class Program
{
    public static void main(String[] args)
    {
        String str =
            "This is/some text/that I am/parsing/using StringTokenizer/.";

        StringTokenizer strTokElem =
            new StringTokenizer(str, "/", true);
        System.out.println("Elements...");
        while (strTokElem.hasMoreElements())
        {
            System.out.println(strTokElem.nextElement());
        }

        StringTokenizer strTokToken =
            new StringTokenizer(str, "/", false);
        System.out.println("Tokens...");
        while (strTokToken.hasMoreTokens())
        {
            System.out.println(strTokToken.nextToken());
        }

        StringTokenizer strTok =
            new StringTokenizer(str, "/", false);
        System.out.println("Count...");
        System.out.println(strTok.countTokens());
    }
}

/*
Output:
Elements...
This is
/
some text
/
that I am
/
parsing
/
using StringTokenizer
/
.
Tokens...
This is
some text
that I am
parsing
using StringTokenizer
.
Count...
6
*/

java.lang.Object

  java.util.StringTokenizer

Community Additions

ADD
Show:
© 2015 Microsoft