Java StringTokenizer - Specify Delimiter example

  1. /*
  2.   Java StringTokenizer - Specify Delimiter example.
  3.   This example shows how a specify a delimiter for StringTokenizer object.
  4.   The default delimiters are
  5.   \t character (tab),
  6.   \n character (new line),
  7.   \r character (carriage return) and
  8.   \f character (form feed).
  9. */
  10.  
  11. import java.util.StringTokenizer;
  12.  
  13. public class StringTokenizerSpecifyDelimiterExample {
  14.  
  15. public static void main(String[] args) {
  16.  
  17. /*
  18.   * There are two ways to specify a delimiter for a StringTokenizer object.
  19.   * 1. At the creating time by specifying in the StringTokenizer constructor
  20.   * 2. Specify it in nextToken() method
  21.   */
  22.  
  23. //1. Using StringTokenizer constructor
  24. StringTokenizer st1 = new StringTokenizer("Java|StringTokenizer|Example 1", "|");
  25.  
  26. //iterate through tokens
  27. while(st1.hasMoreTokens())
  28. System.out.println(st1.nextToken());
  29.  
  30. //2. Using nextToken() method. Note that the new delimiter set remains the
  31. //default after this method call
  32. StringTokenizer st2 = new StringTokenizer("Java|StringTokenizer|Example 2");
  33.  
  34. //iterate through tokens
  35. while(st2.hasMoreTokens())
  36. System.out.println(st2.nextToken("|"));
  37.  
  38. }
  39. }
  40.  
  41. /*
  42. Java
  43. StringTokenizer
  44. Example 1
  45. Java
  46. StringTokenizer
  47. Example 2
  48. */

Post new comment

To combat spam, please enter the code in the image.


Suggested Reading

Oracle Magazine
Contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more.
Cost: FREE

View/Subscribe
NASA Tech Briefs
Features exclusive reports of innovations developed by NASA and its industry partners/contractors that can be applied to develop new/improved products and solve engineering or manufacturing problems.
Cost: FREE

View/Subscribe
FierceBiotech IT
Is a free, easy to read weekly email service that brings must read biotech IT news to senior biotech, pharma, and IT executives.
Cost: FREE

View/Subscribe
Simply SQL
Simply SQL is a practical step-by-step guide to writing SQL.
Cost: FREE

View/Subscribe
Simply JavaScript
Packed with full-color examples, Simply JavaScript is all you need to start programming in JavaScript the right way.
Cost: FREE

View/Subscribe
PCMag.com's What's New Now
Lance Ulanoff, Editor in Chief of the PC Magazine Network, brings you this twice-weekly roundup of the latest top tech stories, the best new product reviews, plus special offers from Ziff Davis and its partners.
Cost: FREE

View/Subscribe

Could not find what you are looking for? Search Java Examples




Feel Tired? Read Jokes & Inspirational Stories, Play Games!