Results 1 to 6 of 6
  1. #1
    yuriyl is offline Member
    Join Date
    Jul 2009
    Posts
    4
    Rep Power
    0

    Default Multi Delimeter String Parsing

    Hello,

    I have a string that contains |% as a delimiter. The StringTokenizer class doesn't seem to be working for this particular case. Is there something else I can use that would parse out this multi character delimiter and also return the delimiter as well?

    So for example

    col1|%col2|%|%

    The output should be
    col1
    |%
    col2
    |%
    |%

    Thanks in advance for any assistance.

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

    Default

    I usually use String.split(...) rather than StringTokenizer. You can't use "|%" as a delimiter without escaping the '|' character since it has meaning in regular expressions. Thus "\\|%" can be used as a delimiter, but even so, the delimiter will be swallowed when you split your String, so the output will be
    col1
    col2


    For instance:
    Java Code:
    public class Fu1 {
      public static void main(String[] args) {
        String test = "col1|%col2|%|%";
        
        String[] tokens = test.split("\\|%", -1);
        for (String token : tokens) {
          System.out.println(token);
        }
      }
    }

  3. #3
    yuriyl is offline Member
    Join Date
    Jul 2009
    Posts
    4
    Rep Power
    0

    Default

    Ok thank you, the thing is that the file that I will be parsing will have some empty values which I still need to capture. If String.split() will swallow the delimiter then I will lose that information. Is this the only option?

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

    Default

    Have you tried calling split with the second parameter, the limit < 0? It should then capture all empty values. Give it a try.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Another thing you can try is a delimiter made up of the more advanced components of regular expressions -- look ahead and look behind. This may allow you to split on your "|%" String and not swallow this String. I'm still learning this, so this code may be wrong, but if you do this, it may work:
    Java Code:
    public class Fu1b {
      public static void main(String[] args) {
        String test = "col1|%col2|%|%";
        
        String[] tokens = test.split("(?=\\|%)|(?<=\\|%)");
        for (String token : tokens) {
          System.out.println(token);
        }
      }
    }
    Last edited by Fubarable; 07-11-2009 at 12:58 AM.

  6. #6
    yuriyl is offline Member
    Join Date
    Jul 2009
    Posts
    4
    Rep Power
    0

    Default

    Ok appreciate it, I will play around with the split function. You can close out this thread.

Similar Threads

  1. Multi-chatroom
    By Mr.Beans in forum Networking
    Replies: 1
    Last Post: 05-15-2009, 11:16 PM
  2. parsing numbers in a string
    By rsoler in forum Advanced Java
    Replies: 4
    Last Post: 03-31-2009, 06:05 AM
  3. Multi Client TCP or UDP
    By hunterbdb in forum Networking
    Replies: 8
    Last Post: 10-17-2008, 04:10 AM
  4. How come multi thread don't look like it?
    By jkhoa in forum Threads and Synchronization
    Replies: 1
    Last Post: 09-22-2007, 04:25 AM

Posting Permissions

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