0

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,

1
  • The StringTokenizer javadocs state StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead. so you should be looking at the answers provided by mcfinnigan and Francois. Commented Dec 22, 2011 at 14:56

3 Answers 3

3

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

;
Sign up to request clarification or add additional context in comments.

Comments

2

Look into String.split()

This should do what you are looking for.

http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

Comments

2

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]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.