Results 1 to 12 of 12
Thread: StringTokenizer question
- 07-15-2009, 03:30 AM #1
Member
- Join Date
- Jul 2009
- Posts
- 56
- Rep Power
- 0
-
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.
- 07-15-2009, 04:49 PM #3
Member
- Join Date
- Jul 2009
- Posts
- 56
- Rep Power
- 0
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?
-
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); } } }
- 07-16-2009, 01:11 AM #5
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
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
- 07-17-2009, 08:15 AM #6
Member
- Join Date
- Jul 2009
- Posts
- 5
- Rep Power
- 0
Hi, this should do the trick.
apply an unified character as the delimiter with replace() - String
:-)
- 07-19-2009, 07:31 PM #7
Member
- Join Date
- Jul 2009
- Posts
- 12
- Rep Power
- 0
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......
- 07-19-2009, 07:40 PM #8
Member
- Join Date
- Jul 2009
- Posts
- 56
- Rep Power
- 0
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?
- 07-20-2009, 01:18 AM #9
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Try it and see what happens.All 1's, 2's, and 3's would be delimiters? Or only where 123 are connected like that?
- 07-20-2009, 05:20 AM #10
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)
- 07-20-2009, 06:34 AM #11
Member
- Join Date
- Jul 2009
- Posts
- 12
- Rep Power
- 0
hello Chasingxsuns,
all 1's , 2's 3's will be delimiters...
- 07-20-2009, 06:36 AM #12
Member
- Join Date
- Jul 2009
- Posts
- 12
- Rep Power
- 0
Similar Threads
-
StringTokenizer Question
By coder09 in forum New To JavaReplies: 5Last Post: 02-26-2009, 12:46 AM -
Question: BufferReader/StringTokenizer
By VinceGuad in forum New To JavaReplies: 1Last Post: 01-26-2009, 05:50 PM -
Help with StringTokenizer!
By ookie833 in forum New To JavaReplies: 13Last Post: 12-14-2008, 04:09 PM -
StringTokenizer
By carderne in forum New To JavaReplies: 1Last Post: 01-26-2008, 08:19 PM -
StringTokenizer
By Java Tip in forum Java TipReplies: 0Last Post: 11-03-2007, 09:24 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks