Results 1 to 3 of 3
  1. #1
    javaplus is offline Member
    Join Date
    Nov 2007
    Posts
    97
    Rep Power
    0

    Default How to use StringTokenizer for multiple tokens

    Here is the situation:

    I have a String and I want to get tokens from it. I have 3 delimiters which are:
    " " (space)
    "," (comma)
    ";" (semi colon)

    Is there a way to use StringTokenizer with these 3 demiliters?

    Please look into this.

    Thanks in advance.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    String s = "The StringTokenizer methods do not distinguish " +
               "among identifiers, numbers, and quoted strings, " +
               "nor do they recognize and skip comments; it is " +
               "a legacy class.";
    System.out.println("s = " + s);
    String delims = " ,;";
    StringTokenizer tokens = new StringTokenizer(s, delims);
    while(tokens.hasMoreTokens())
        System.out.println(tokens.nextToken());

  3. #3
    javaplus is offline Member
    Join Date
    Nov 2007
    Posts
    97
    Rep Power
    0

    Default

    Perfect. It worked. Thanks.

    I have another question. What if my token is composed of more than one characters?

    Java Code:
    String s= "I am new to Java. Are you good in Java, if yes please help me.";
    String delim = "Java";
    StringTokenizer tokens = new StringTokenizer(s, delim);
    while(tokens.hasMoreTokens())
        System.out.println(tokens.nextToken());
    Output:
    Java Code:
    Welcome
    I 
    m new to 
    . Are you good in 
    , if yes ple
    se help me.
    Actually "Java" is not treated as a token but 4 different tokens (each character is a token). How to handle this?

Similar Threads

  1. Getting tokens using Scanner class
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-05-2008, 09:11 AM
  2. tokens
    By Gilgamesh in forum New To Java
    Replies: 5
    Last Post: 12-02-2007, 11:30 PM
  3. tokens
    By Gilgamesh in forum New To Java
    Replies: 3
    Last Post: 11-25-2007, 02:39 AM
  4. StringTokenizer
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-08-2007, 08:48 AM
  5. StringTokenizer
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-03-2007, 09:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •