Results 1 to 6 of 6
Thread: Multi Delimeter String Parsing
- 07-10-2009, 11:02 PM #1
Member
- Join Date
- Jul 2009
- Posts
- 4
- Rep Power
- 0
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.
-
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); } } }
- 07-10-2009, 11:38 PM #3
Member
- Join Date
- Jul 2009
- Posts
- 4
- Rep Power
- 0
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?
-
Have you tried calling split with the second parameter, the limit < 0? It should then capture all empty values. Give it a try.
-
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.
- 07-13-2009, 03:34 PM #6
Member
- Join Date
- Jul 2009
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
Multi-chatroom
By Mr.Beans in forum NetworkingReplies: 1Last Post: 05-15-2009, 11:16 PM -
parsing numbers in a string
By rsoler in forum Advanced JavaReplies: 4Last Post: 03-31-2009, 06:05 AM -
Multi Client TCP or UDP
By hunterbdb in forum NetworkingReplies: 8Last Post: 10-17-2008, 04:10 AM -
How come multi thread don't look like it?
By jkhoa in forum Threads and SynchronizationReplies: 1Last Post: 09-22-2007, 04:25 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks