Reputation: 1
I need to tokenize a string using a delimiter.
StringTokenizer
is capable of tokenizing the string with given delimiter. But, when there are two consecutive delimiters in the string, then it is not considering it as a token.
Thanks in advance for you help
Regards,
Upvotes: 0
Views: 1289
Reputation: 20920
The second parameter to the constructor of StringTokenizer
object is just a string containing all delimiters that you require.
StringTokenizer st = new StringTokenizer(str, "@!");
In this case, there are two delimiters both @
and !
Consider this example :
String s = "Hello, i am using Stack Overflow;";
System.out.println("s = " + s);
String delims = " ,;";
StringTokenizer tokens = new StringTokenizer(s, delims);
while(tokens.hasMoreTokens())
System.out.println(tokens.nextToken());
Here you would get an output similar to this with 3 delimiters :
Hello
,
i
am
using
Stack
Overflow
;
Upvotes: 3
Reputation: 11638
Use the split() method of java.lang.String and pass it a regular expression which matches your one or more delimiter condition.
for e.g. "a||b|||c||||d"
could be tokenised with split("\\|{2,}");
with the resulting array [a,b,c,d]
Upvotes: 2
Reputation: 330
Look into String.split()
This should do what you are looking for.
http://docs.oracle.com/javase/6/docs/api/java/lang/String.html
Upvotes: 2