Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

java.util.StringTokenizer

JavaFAQ Home » Text processing Go to all tips in Text processing


Bookmark and Share

Java: java.util.StringTokenizer

Purpose

The java.util.StringTokenizer class is used to break strings into tokens (words, numbers, operators, or whatever). A more powerful solution is to use regular expressions, which have been added to Java 1.4.

A StringTokenizer constructor takes a string to break into tokens and returns a StringTokenizer object for that string. Each time its nextToken() method is called, it returns the next token in that string. If you don't specify the delimiters (separator characters), blanks are the default.

Constructors

StringTokenizer st = new StringTokenizer(s);
Creates a StringTokenizer for the String s that uses whitespace (blanks, tabs, newlines, returns, form feeds) as delimiters.
StringTokenizer st = new StringTokenizer(s, d);
Creates a StringTokenizer for the String s using delimiters from the String d.
StringTokenizer st = new StringTokenizer(s, d, f);
Creates a StringTokenizer for the String s using delimiters from the String d. If the boolean f is true, each delimiter character will also be returned as a token.

Common Methods

Assume that st is a StringTokenizer.
  • st.hasMoreTokens() -- Returns true if there are more tokens.
  • st.nextToken() -- Returns the next token as a String.
  • st.countTokens() -- Returns the int number of tokens. This can be used to allocate an array before starting, altho it can be inefficient for long strings because it has to scan the string once just to get this number. Using a Vector and converting it to an array at the end may be a better choice.

Example: Find the longest word in a String

This code finds the longest word (characters separated by delimiter characters) in the String s, using blanks, commas, and tabs as delimiters.
// Assume s contains a string of words
String longestWord = "";
StringTokenizer st = new StringTokenizer(s, " ,	");
while (st.hasMoreTokens()) {
    String w = st.nextToken();
    if (w.length() > longestWord.length()) {
        longestWord = w;
    }
}

 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!
Custom Search



Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy