Forums Register Login
empty tokens in String Tokenizer
I am reading a String with "," as delimiter.
I use StringTokenizer to read values from String.
But there is a chance that I get no values for some tokens .
eg: 111,222,,444,,666
I have a problem in reading empty tokens.
StringTokenizer class is not considering empty tokens.
I get output as 111 222 444 666
Is there anyway I get that null values?
Please advice me.
If you are using 1.4, look at String.split(). I think it gives you a blank space for empty tokens, but I can't remember for sure. Look at the api to be sure.
I ran into a similar problem using a pipe-delimeted record format using jdk1.3. The only way I was able to resolve this issue was to construct a StringTokenizer where the delimeters are also counted as tokens, and adding logic to find adjacent tokens (which have no value between them), and also for the 1st and last valuebeing null (in which the 1st or last charater of the string is also a delimeter)
Here is the code I came up with....
P.S. The code actually creates a hashmap with the key being the Integer position of the token, and the value being the token.
Assorted past discussions of this topic can be found here, here, and here.

This thread has been viewed 1827 times.

All times above are in ranch (not your local) time.
The current ranch time is
Dec 18, 2018 06:57:19.