Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using multiple delimiters with StringTokenizer

I'd like to know how I can use multiple delimiters with StringTokenizer in java. For example one of these !,*,/,^ will occur as a delimiter. Also there will only be one at a time.

like image 750
nikhil Avatar asked Feb 25 '12 18:02

nikhil


People also ask

Is StringTokenizer deprecated?

StringTokenizer is a legacy class (i.e. there is a better replacement out there), but it's not deprecated.

What is the default delimiter in StringTokenizer class?

Constructs a string tokenizer for the specified string. The tokenizer uses the default delimiter set, which is " \t\n\r\f" : the space character, the tab character, the newline character, the carriage-return character, and the form-feed character.

What happens if no delimiter is specified in the StringTokenizer constructor?

Constructors of the StringTokenizer Class It creates StringTokenizer with specified string and delimiter. It creates StringTokenizer with specified string, delimiter and returnValue. If return value is true, delimiter characters are considered to be tokens. If it is false, delimiter characters serve to separate tokens.


2 Answers

Use the constructor with two arguments, where the second is the delimiters.

StringTokenizer tokenizer = new StringTokenizer(yourString, "!*^/");
like image 189
MByD Avatar answered Oct 07 '22 19:10

MByD


You can use String.split() method because it takes regex as a parameter. You can specify Regex such that it can split the string based upon one of these deliminators.

like image 25
JProgrammer Avatar answered Oct 07 '22 20:10

JProgrammer