•   Basic of Java
  •   OOPs Concepts String
  • Java String


    Java String Methods


    StringTokenizer in Java

    The java.util.StringTokenizer class allows you to break a string into tokens. It is simple way to break string.

    It doesn't provide the facility to differentiate numbers, quoted strings, identifiers etc. like StreamTokenizer class. We will discuss about the StreamTokenizer class in I/O chapter.

    Constructors of StringTokenizer class

    There are 3 constructors defined in the StringTokenizer class.

    Constructor Description
    StringTokenizer(String str) creates StringTokenizer with specified string.
    StringTokenizer(String str, String delim) creates StringTokenizer with specified string and delimeter.
    StringTokenizer(String str, String delim, boolean returnValue) creates StringTokenizer with specified string, delimeter and returnValue. If return value is true, delimiter characters are considered to be tokens. If it is false, delimiter characters serve to separate tokens.

    Methods of StringTokenizer class

    The 6 useful methods of StringTokenizer class are as follows:

    Public method Description
    boolean hasMoreTokens() checks if there is more tokens available.
    String nextToken() returns the next token from the StringTokenizer object.
    String nextToken(String delim) returns the next token based on the delimeter.
    boolean hasMoreElements() same as hasMoreTokens() method.
    Object nextElement() same as nextToken() but its return type is Object.
    int countTokens() returns the total number of tokens.

    Simple example of StringTokenizer class

    Let's see the simple example of StringTokenizer class that tokenizes a string "my name is khan" on the basis of whitespace.

    Flash Soon


    Output:

    Example of nextToken(String delim) method of StringTokenizer class

    Flash Soon


    Output:

    StringTokenizer class is deprecated now. It is recommended to use split() method of String class or regex (Regular Expression).

    Copyright 2017 Design& Development