Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-11-2009, 12:02 AM
Member
 
Join Date: Jul 2009
Posts: 3
Rep Power: 0
yuriyl is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 07-11-2009, 12:33 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,489
Rep Power: 8
Fubarable is on a distinguished road
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:
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);
    }
  }
}
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-11-2009, 12:38 AM
Member
 
Join Date: Jul 2009
Posts: 3
Rep Power: 0
yuriyl is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-11-2009, 01:49 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,489
Rep Power: 8
Fubarable is on a distinguished road
Default
Have you tried calling split with the second parameter, the limit < 0? It should then capture all empty values. Give it a try.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-11-2009, 01:56 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,489
Rep Power: 8
Fubarable is on a distinguished road
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:
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 01:58 AM.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-13-2009, 04:34 PM
Member
 
Join Date: Jul 2009
Posts: 3
Rep Power: 0
yuriyl is on a distinguished road
Default
Ok appreciate it, I will play around with the split function. You can close out this thread.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multi-chatroom Mr.Beans Networking 1 05-16-2009 12:16 AM
parsing numbers in a string rsoler Advanced Java 4 03-31-2009 07:05 AM
Multi Client TCP or UDP hunterbdb Networking 8 10-17-2008 05:10 AM
How come multi thread don't look like it? jkhoa Threads and Synchronization 1 09-22-2007 05:25 AM


All times are GMT +2. The time now is 06:34 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org