gene b.
gene b.

Reputation: 11984

Multiple String Delimiters in StringTokenizer

I am tokenizing an HTML block which has to be split by these whole tag strings:

<dd>
</dd>
<dt>
</dt>

I can't specify

StringTokenizer st = new StringTokenizer(block,"<dt></dt><dd></dd>");
st.nextToken();

These aren't a collection of chars, it's a collection of whole strings. Is there an elegant way to tokenize this?

Upvotes: 1

Views: 1209

Answers (1)

Justin Kaufman
Justin Kaufman

Reputation: 261

From the JavaDoc:

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, if you have a String block that contains the data to tokenize. Do something like

String[] tokens = block.split("(<dd>)|(</dd>)|(<dt>)|(</dt>)");

and then your tokens will be the elements in the array tokens

Upvotes: 2

Related Questions