Java Tutorials-Taking String apart using the StringTokenizer class

preview_player
Показать описание
Taking strings apart using the StringTokenizer class

The StringTokenizer class is used to tokenize or break strings into tokens or parts.
A StringTokenizer normally breaks the String into tokens at what we would think of as “word boundaries” in European languages. It breaks the String at delimiters. A delimiter is a character which separates tokens. The delimiter can be passed to the argument when the stringTokenizer object is constructed.
The default set of delimiters of the StringTokenizer are space character, tab, newline character, carriage return character, and form-feed character
Constructors of the StringTokenizer

1. StringTokenizer(String str)
This constructor takes the String as argument and breaks the String using default delimiters.
The default delimiters of the StringTokenizer class are space, tab character(\t), newline. character(\n), return carriage character(\r), and form-feed character(\f).

2. StringTokenizer(String str, String “delimiter”)
This constructor takes in two arguments. It takes the String to tokenize and another string as delimiter as its two arguments. The delimiter reset the default delimiters. These become the new delimiters around which the string will be split or tokenized.

3. StringTokenizer(String str, String “delimiter”, Boolean value)
This constructor takes in three arguments. It takes the String to tokenize, another string as delimiter and a Boolean value as its three arguments. The Boolean value tells the constructor whether to consider the delimiters as part of the tokens or not. If the Boolean value is true, the delimiters are considered as tokens.
Methods of the StringTokenizer class
1. hasMoreTokens() : Boolean
It checks if String has more tokens. It returns true if String has more tokens.
2. nextToken(): String
It returns the next token from this string tokenizer
3. nextElement(): Object
it returns the next token from this string tokenizer except that its declared return value is an object not a string.

4. hasMoreElements():String
This is similar to the hasMoreTokens() method. It returns true if String has more tokens.
5. countTokens(): int
It counts the number of tokens. It returns an int number representing the number of tokens in the String.
Рекомендации по теме