Results 1 to 12 of 12
  1. #1
    Chasingxsuns is offline Member
    Join Date
    Jul 2009
    Posts
    56
    Rep Power
    0

    Default StringTokenizer question

    Is there a way I can make a StringTokenizer have more than one delimiter? Basically I want the delimiters to be (, /, and ). I don't see a method like setDelimiter or anything, but I figured I would ask on here.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    29

    Default

    You might want to look instead at using the String.split(...) method which takes a regular expression String as its delimiter, and these can get quite complex. Another option is the java.util.Scanner object that also can take a regex delimiter.

  3. #3
    Chasingxsuns is offline Member
    Join Date
    Jul 2009
    Posts
    56
    Rep Power
    0

    Default

    I'm a little confused on the split(...) method. Is the String regex as a parameter what determines the delimiters? The examples only give regex as one character like ":" or "o". I can see how it is useful, but how can I use this for multiple delimiters?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    29

    Default

    Say you want to split a String on either the % character or the ^ character. You could create a regex that accepts either character, but you'll have to escape the "^" String with "\\^" since "^" has meaning in regex. Something like this:
    String regex = "[%\\^]";

    For example:
    Java Code:
    public class MultipleSplit {
      public static void main(String[] args) {
        String test = 
          "What we have here%is a failure to communicate.^Make everything as " +
        		"simple as possible,%but not simpler.";
        
        String regex = "[%\\^]";
        String[] tokens = test.split(regex);
        for (String token : tokens) {
          System.out.println(token);
        }
      }
    }

  5. #5
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    10

    Default

    stupid confusing backslashes... had to look up why there's two instead of just one and found out that to use a single backslash is "\\\\"... nice

  6. #6
    lada314 is offline Member
    Join Date
    Jul 2009
    Posts
    5
    Rep Power
    0

    Default

    Hi, this should do the trick.

    apply an unified character as the delimiter with replace() - String

    :-)

  7. #7
    Harpreet1111 is offline Member
    Join Date
    Jul 2009
    Posts
    12
    Rep Power
    0

    Default

    hello Chasingxsuns,

    u can pass a delimiter string in the constructor of StringTokenizer class itself.

    StringTokenizer st=new StringTokenizer(String str,",//)");

    using this parse str and enjoy......

  8. #8
    Chasingxsuns is offline Member
    Join Date
    Jul 2009
    Posts
    56
    Rep Power
    0

    Default

    So every character passed in the constructor is treated as a delimiter? If I put
    StringTokenizer st = new StringTokenizer(String str, "123")

    All 1's, 2's, and 3's would be delimiters? Or only where 123 are connected like that?

  9. #9
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    10

    Default

    All 1's, 2's, and 3's would be delimiters? Or only where 123 are connected like that?
    Try it and see what happens.

  10. #10
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    10

    Default

    You're not suppose to use StringTokenizer as it old and might become "deprecated" in future versions of java. Just use String split, scanner, or pattern-matcher.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  11. #11
    Harpreet1111 is offline Member
    Join Date
    Jul 2009
    Posts
    12
    Rep Power
    0

    Default

    hello Chasingxsuns,

    all 1's , 2's 3's will be delimiters...

  12. #12
    Harpreet1111 is offline Member
    Join Date
    Jul 2009
    Posts
    12
    Rep Power
    0

    Default

    hello Chasingxsuns,

    all 1's , 2's 3's will act as delimiters....

Similar Threads

  1. StringTokenizer Question
    By coder09 in forum New To Java
    Replies: 5
    Last Post: 02-26-2009, 01:46 AM
  2. Question: BufferReader/StringTokenizer
    By VinceGuad in forum New To Java
    Replies: 1
    Last Post: 01-26-2009, 06:50 PM
  3. Help with StringTokenizer!
    By ookie833 in forum New To Java
    Replies: 13
    Last Post: 12-14-2008, 05:09 PM
  4. StringTokenizer
    By carderne in forum New To Java
    Replies: 1
    Last Post: 01-26-2008, 09:19 PM
  5. StringTokenizer
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-03-2007, 10: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
  •