StringTokenizer in Java

StringTokenizer in Java

String tokenizer class is useful to break string into tokens(pieces). These tokens are then stored in the StringTokenizer object from where they can be retrieved. The code to create an object to StringTokenizer class is :

In the preceding statement, the actual string is broken into pieces at the position marked by a group of characters called ‘delimeter’. For example, to break the String wherever a comma is found we can write –

Similarly, to break the String based on comma/colon (,/:) or both, we can use –

StringTokenizer Class Methods

  • int countTokens() : This method counts and returns the number of tokens available in a StringTokenizer object.
  • boolean hasMoreTokens(): This method tests if there are more tokens available in the StringTokenizer object or not. If next token is there then it returns true.
  • String nextToken(): This method returns the next token from the StringTokenizer.

Example to demonstrate splitting of string using StringTokenizer class :-


Stay tuned for more updates !

Leave a Reply

Your email address will not be published. Required fields are marked *