Java StringTokenizer - Walking Techie

Blog about Java programming, Design Pattern, and Data Structure.

Sunday, March 19, 2017

Java StringTokenizer

StringTokenizer

The string tokenizer class allows an application to break a string into tokens. The tokenization method is much simpler than * the one used by the StreamTokenizer class. The StringTokenizer methods do not distinguish among identifiers, numbers, and quoted strings, nor do they recognize and skip comments.

Here is the topic that will cover in this post.

  1. StringTokenizer class Declaration
  2. StringTokenizer constructors
  3. StringTokenizer class Methods
  4. Example

Java StringTokenizer class Declaration

Java StringTokenizer implements Enumeration. It is available in java since JDK 1.0.

public class StringTokenizer implements Enumeration<Object>

Java StringTokenizer constructors

StringTokenizer has three constructors.

Constructor Description
public StringTokenizer(String str) Constructs a string tokenizer for the specified string. The tokenizer uses the default delimiter set, which is " \t\n\r\f": the space character, the tab character, the newline character, the carriage-return character, and the form-feed character. Delimiter characters themselves will not be treated as tokens.
public StringTokenizer(String str, String delim) Constructs a string tokenizer for the specified string. The characters in the delim argument are the delimiters for separating tokens. Delimiter characters themselves will not be treated as tokens.
public StringTokenizer(String str, String delim, boolean returnDelims) Constructs a string tokenizer for the specified string. All characters in the delim argument are the delimiters for separating tokens. If the returnDelims flag is true, then the delimiter characters are also returned as tokens. Each delimiter is returned as a string of length one. If the flag is false, the delimiter characters are skipped and only serve as separators between tokens.
package com.walking.techie;

import java.util.StringTokenizer;

public class STConstructorDemo {

  public static void main(String[] args) {
    String string = "Hello Walking techie , how are you ?";
    StringTokenizer stringTokenizer = new StringTokenizer(string);

    System.out.println("*****Split by space*****");
    while (stringTokenizer.hasMoreTokens()) {
      System.out.println(stringTokenizer.nextToken());
    }

    System.out.println("*****Split by comma and space*****");
    StringTokenizer st1 = new StringTokenizer(string, ", ");
    while (st1.hasMoreTokens()) {
      System.out.println(st1.nextToken());
    }

    System.out.println("Split by comma and space, including delimiter as tokens");
    StringTokenizer st2 = new StringTokenizer(string, ", ", true);
    while (st2.hasMoreTokens()) {
      System.out.println(st2.nextToken());
    }
  }
}

Output of above program is shown below:

*****Split by space*****
Hello
Walking
techie
,
how
are
you
?
*****Split by comma and space*****
Hello
Walking
techie
how
are
you
?
Split by comma and space, including delimiter as tokens
Hello

Walking

techie

,

how

are

you

?

Java StringTokenizer methods

Method Description
public boolean hasMoreTokens() Returns true if there are more tokens available from this tokenizer's string. Returns false if there are none.
public String nextToken() Returns the next token from this string tokenizer.
public String nextToken(String delim) Returns the next token in this string tokenizer's string. First, the set of characters considered to be delimiters by this StringTokenizer object is changed to be the characters in the string delim. Then the next token in the string after the current position is returned. The current position is advanced beyond the recognized token. The new delimiter set remains the default after this call.
public boolean hasMoreElements() Returns the same value as the hasMoreTokens method.
public Object nextElement() Returns the same value as the nextToken method, except that its return type is Object rather than String.
public int countTokens() Calculates the number of times that this tokenizer's nextToken method can be called before it generates an exception. In other words, returns the total number of tokens.

Example

Here is few simple program of StringTokenizer.

package com.walking.techie;

import java.util.StringTokenizer;

public class StringTokenizerDemo {

  public static void main(String[] args) {
    String string = "my*, name is*,*khan";
    StringTokenizer stringTokenizer = new StringTokenizer(string);
    boolean flag = true;
    while (stringTokenizer.hasMoreTokens()) {
      if (flag) {
        System.out.println(stringTokenizer.nextToken(","));
        flag = false;
      } else {
        System.out.println(stringTokenizer.nextToken());
      }
    }
  }
}

Output of above program is shown below:

my*
 name is*
*khan
package com.walking.techie;

import java.util.StringTokenizer;

public class StringTokenizerDemo {

  public static void main(String[] args) {
    String string = "http://127.0.0.1:8080/";
    StringTokenizer st = new StringTokenizer(string,":./");

    while (st.hasMoreTokens()) {
        System.out.println(st.nextToken());
    }
  }
}

Output of above program is shown below:

http
127
0
0
1
8080

1 comment :

  1. factorial hundred In the last few days, the “factorial of 100” is one of the top subjects and a lot of maths geeks compute it using voice assistants such as Alexa, Shiri, etc.
    factorial-of-hundred In the last few days, the “factorial of 100” is one of the top subjects and a lot of maths geeks compute it using voice assistants such as Alexa, Shiri, etc.

    ReplyDelete