StringTokenizer class in JAVA
StringTokenizer is a class which is used to tokenize a given string .
Mainly it has 3 constructors.
1 . StringTokenizer(String str)
str is a string to be tokenized. Considers default delimiter like space,line break,
tab, carraige return and form feed.
2 . StringTokenizer(String str,String delimiters)
str is a string to be tokenized and delimiter is a set of delimiter used to tokenize given string.
3 . StringTokenizer(String str,String delimiters,boolean flag)
str is a string to be tokenized, delimiters is set of delimiters used to tokenize
given string, flag is a boolean value "true" or "false". If flag is false set of delimiters are used to serve separate tokens and if flag is true then set of delimiters are considered as tokens.
for example
import java.util.*;
public class StringTokenizerTest
{
public static void main(String args[])
{
System.out.println("Using first constructor ");
StringTokenizer stringTokenizer1 =
new StringTokenizer("Hello How are you");
while (stringTokenizer1.hasMoreTokens())
System.out.println(stringTokenizer1.nextToken());
System.out.println("Using Second Constructor ");
StringTokenizer stringTokenizer2 =
new StringTokenizer("String : Tokenizer : Class", " :");
while (stringTokenizer2.hasMoreTokens())
System.out.println(stringTokenizer2.nextToken());
System.out.println("Using Third(1) Constructor ");
StringTokenizer stringTokenizer3 =
new StringTokenizer("String : Tokenizer : Class", " :", true);
while (stringTokenizer3.hasMoreTokens())
System.out.println(stringTokenizer3.nextToken());
System.out.println("Using Third(2) Constructor ");
StringTokenizer stringTokenizer4 =
new StringTokenizer("String : Tokenizer : Class", " :", false);
while (stringTokenizer4.hasMoreTokens())
System.out.println(stringTokenizer4.nextToken());
}
}
output:
Mainly it has 3 constructors.
1 . StringTokenizer(String str)
str is a string to be tokenized. Considers default delimiter like space,line break,
tab, carraige return and form feed.
2 . StringTokenizer(String str,String delimiters)
str is a string to be tokenized and delimiter is a set of delimiter used to tokenize given string.
3 . StringTokenizer(String str,String delimiters,boolean flag)
str is a string to be tokenized, delimiters is set of delimiters used to tokenize
given string, flag is a boolean value "true" or "false". If flag is false set of delimiters are used to serve separate tokens and if flag is true then set of delimiters are considered as tokens.
for example
import java.util.*;
public class StringTokenizerTest
{
public static void main(String args[])
{
System.out.println("Using first constructor ");
StringTokenizer stringTokenizer1 =
new StringTokenizer("Hello How are you");
while (stringTokenizer1.hasMoreTokens())
System.out.println(stringTokenizer1.nextToken());
System.out.println("Using Second Constructor ");
StringTokenizer stringTokenizer2 =
new StringTokenizer("String : Tokenizer : Class", " :");
while (stringTokenizer2.hasMoreTokens())
System.out.println(stringTokenizer2.nextToken());
System.out.println("Using Third(1) Constructor ");
StringTokenizer stringTokenizer3 =
new StringTokenizer("String : Tokenizer : Class", " :", true);
while (stringTokenizer3.hasMoreTokens())
System.out.println(stringTokenizer3.nextToken());
System.out.println("Using Third(2) Constructor ");
StringTokenizer stringTokenizer4 =
new StringTokenizer("String : Tokenizer : Class", " :", false);
while (stringTokenizer4.hasMoreTokens())
System.out.println(stringTokenizer4.nextToken());
}
}
output:
Using first constructor Hello How are you Using Second Constructor String Tokenizer Class Using Third(1) Constructor String : Tokenizer : Class Using Third(2) Constructor String Tokenizer Class
0 comments: