How do I use StringTokenizer to split a string?

Category: java.util, viewed: 59498 time(s).
import java.util.StringTokenizer;

public class StringTokenizerSample
{
    public static void main(String[] args)
    {
        StringTokenizer st =
                new StringTokenizer("a stringtokenizer sample");

        // get how many tokens inside st object
        System.out.println("tokens count: " + st.countTokens());

        // iterate st object to get more tokens from it
        while (st.hasMoreElements())
        {
            String token = st.nextElement().toString();
            System.out.println("token = " + token);
        }

        // split a date string using a forward slash as
        // delimiter
        st = new StringTokenizer("2005/12/15", "/");
        while (st.hasMoreElements())
        {
            String token = st.nextToken();
            System.out.println("token = " + token);
        }
    }
}

The above code is an example of using StringTokenizer to split a string. In the current JDK this class is discourageg to be used, using instead the String.split(...) method or using a new java.util.regex package.

Here is the result of this sample code:

tokens count: 3
token = a
token = stringtokenizer
token = sample
token = 2005
token = 12
token = 15
Click here to lend your support to: Kode Java Org and make a donation at www.pledgie.com !

 

Can't find what you are looking for? Join our FORUMS and ask some questions!
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Sponsored Links

Our Friends

Statistics

Locations of visitors to this page
visitor stats