210 likes | 438 Views
StringTokenizer Class. StringTokenizer Class java.util.StringTokenizer;. Used for Parsing a Formatted Input String Parsing : Division of text into set of discrete parts known as tokens Token convey a semantic meaning Individual tokens are separated by Delimiters
E N D
StringTokenizer Classjava.util.StringTokenizer; • Used for Parsing a Formatted Input String • Parsing : Division of text into set of discrete parts known as tokens • Token convey a semantic meaning • Individual tokens are separated by Delimiters • Delimiters can be specified by a delimiter String and each character in delimiter String is treated as a separate delimiter • A provision is provided by which you can treat individual delimiters as tokens also • Space , Tab, newline and carriage return << Default Set of Delimiters>>
Example String str = "BITS C342,83,Object Oriented Programming,Lecture Section 3;MWF 9,Sunita;Bansal,3160“; If Only Comma (,) is taken as delimiter: NO OF TOKENS = 6 Tokens : { BITS C342} {83} {Object Oriented Programming} {Lecture Section 3; MWF 9} {Sunita;Bansal} {3160} If Only Semicolon (;) is taken as delimiter NO OF TOKENS = 3 Tokens : ? If Both Comma(,) and Semicolon (;) are taken as delimiters: NO OF TOKENS = 8 Tokens : ? Delimitersthemselves are not taken as Tokens
StringTokennizer Constructors • StringTokennizer(String str) • << str> String to be tokennized. • No delimiter is specified so default set delimiters will beassumed. • Delimiters will not be considered as tokens • StringTokennizer(String str, String delimiters) • << delimiters>> specify a delimiter String. Single or multiple characters can be specified as delimiters. • If the delimiter String is “:,;” then colon, comma and semicolon are used as delimiters • Individual character in the the delimiter String is separately treated as a delimiters. • By Default Delimiters will not be considered as tokens
StringTokennizer Constructors cont… • StringTokennizer(String str, String delimiters, boolean delimAsToken) • First Two parameters are same as previous constructor. Third parameter delimAsToken indicates whether delimiters are to be taken as tokens or not. • Delimiters will be considered as tokens if delimAsToken is true otherwise will be ignored if false
StringTokenizer Methods • int countTokens() • Counts the number of tokens in StringTokennizer based upon delimiters • boolean hasMoreTokens() / boolean hasMoreElements() • Helpful in parsing a String. • Returns true if there are one or more tokens left otherwise false. • String nextToken() • Returns the next token in String form • Used in conjunction with hasMoreTokens() method • Object nextElement() • Same as nextToken() but returns next Token in Object Form not in String Form
String str = "BITS C342,83,Object Oriented Programming,Lecture Section 3;MWF 9,Sunita;Bansal,3160"; StringTokenizer strT1 = new StringTokenizer(str); // No Delimiters Specified System.out.println("Number Of Tokens :"+strT1.countTokens()); System.out.println("Individual Tokens"); while(strT1.hasMoreTokens()) System.out.println(strT1.nextToken()); Number Of Tokens :7 Individual Tokens BITS C342,83,Object Oriented Programming,Lecture Section 3;MWF 9,Sunita;Bansal,3160
String str = "BITS C342,83,Object Oriented Programming,Lecture Section 3;MWF 9,Sunita;Bansal,3160"; StringTokenizer strT1 = new StringTokenizer(str,”;”); // Semicolon (;) is Specified as delimiter System.out.println("Number Of Tokens :"+strT1.countTokens()); System.out.println("Individual Tokens"); while(strT1.hasMoreTokens()) System.out.println(strT1.nextToken()); Number Of Tokens :3 Individual Tokens BITS C342,83,Object Oriented Programming,Lecture Section 3 MWF 9,Sunita Bansal,3160
String str = "BITS C342,83,Object Oriented Programming,Lecture Section 3;MWF 9,Sunita;Bansal,3160"; StringTokenizer strT1 = new StringTokenizer(str,”,”); // Comma (,) is Specified as delimiter System.out.println("Number Of Tokens :"+strT1.countTokens()); System.out.println("Individual Tokens"); while(strT1.hasMoreTokens()) System.out.println(strT1.nextToken()); Number Of Tokens :6 Individual Tokens BITS C342 83 Object Oriented Programming Lecture Section 3;MWF 9 Sunita;Bansal 3160
String str = "BITS C342,83,Object Oriented Programming,Lecture Section 3;MWF 9,Sunita;Bansal,3160"; StringTokenizer strT1 = new StringTokenizer(str,”;,”); // Both Semicolon (;) and Comma (,) are Specified as delimiters System.out.println("Number Of Tokens :"+strT1.countTokens()); System.out.println("Individual Tokens"); while(strT1.hasMoreTokens()) System.out.println(strT1.nextToken()); Number Of Tokens :8 Individual Tokens BITS C342 83 Object Oriented Programming Lecture Section 3 MWF 9 Sunita Bansal 3160
String str = "BITS C342,83,Object Oriented Programming,Lecture Section 3;MWF 9,Sunita;Bansal,3160"; StringTokenizer strT1 = new StringTokenizer(str,”;,”, true); // Both Semicolon (;) and Comma (,) are Specified as delimiters // Delimiters then selves are tokens now System.out.println("Number Of Tokens :"+strT1.countTokens()); System.out.println("Individual Tokens"); while(strT1.hasMoreTokens()) System.out.println(strT1.nextToken());
Number Of Tokens :15 Individual Tokens BITS C342 , 83 , Object Oriented Programming , Lecture Section 3 ; MWF 9 , Sunita ; Bansal , 3160