1 / 12

StringTokenizer Class

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

tarika
Download Presentation

StringTokenizer Class

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. StringTokenizer Class

  2. 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>>

  3. Example String str = "BITS C342,83,Object Oriented Programming,Lecture Section 3;MWF 9,Pankaj;Vyas,3160“; If Only Comma (,) is taken as delimiter: NO OF TOKENS = 6 Tokens : { BITS C342} {83} {Object Oriented Programming} {Lecture Section 3; MWF 9} {Pankaj;Vyas} {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

  4. 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

  5. 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

  6. 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

  7. String str = "BITS C342,83,Object Oriented Programming,Lecture Section 3;MWF 9,Pankaj;Vyas,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,Pankaj;Vyas,3160

  8. String str = "BITS C342,83,Object Oriented Programming,Lecture Section 3;MWF 9,Pankaj;Vyas,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,Pankaj Vyas,3160

  9. String str = "BITS C342,83,Object Oriented Programming,Lecture Section 3;MWF 9,Pankaj;Vyas,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 Pankaj;Vyas 3160

  10. String str = "BITS C342,83,Object Oriented Programming,Lecture Section 3;MWF 9,Pankaj;Vyas,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 Pankaj Vyas 3160

  11. String str = "BITS C342,83,Object Oriented Programming,Lecture Section 3;MWF 9,Pankaj;Vyas,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());

  12. Number Of Tokens :15 Individual Tokens BITS C342 , 83 , Object Oriented Programming , Lecture Section 3 ; MWF 9 , Pankaj ; Vyas , 3160

More Related