My application needs to parse a String with a double delimeter "::" into String arrays. The code I am using is as follows. String strTemp ="999::adm::888::mgr::security::flag"; StringTokenizer strToken = new StringTokenizer(strTemp,"::"); String tempArray[] = new String[7]; int index =0; while (strToken.hasMoreTokens()) { System.out.println("tempArray "+index+" :: "+(String)strToken.nextToken()); index++; } This format of String strTemp is working fine and I am getting six String arrays. But when any value inbetween the double delimeter is missing,say example the data "888" is missing ,then strTemp ="999::adm::::mgr::security::flag"; My requirement is , now also I should get the same no of arrays of Strings , inwhich array[2] should be null. But I am getting only 5 arrays of Strings where I am seeing all the array indexs after the missed String is shifted. StringTokenizer approach is not working at all . I knew , we can solve this issue by doing IndexOf method of String Class..But I feel, this approach is a very basic one which consumes more no of coding lines.. Friends, anybody guide me with an optimised approach?
Hi... Thanks to ur reply. But the code u given is throwing compilation error since there is no split method in String Class of Java. I am using jdk1.3.
It's easy enough to see the implementation of the StringTokenizer class, especially if you have an IDE like JBuilder. Just click on StringTokenizer somewhere in you code and press Ctrl-Enter. This quickly shows that the StringTokenizer approach is never going to work in the case where there is nothing between the delimiters. This is because the code loops until it finds something that isn't a delimiter character. A neat approach, if you have the programming skills to do it, would be to do a copy-paste-modify job to create you own version of the StringTokenizer class and save it in your own package. John
Mr Singh M., The source code you given also is behaving like ordinary StringTokenizer as for as my issue is concerned. Can u help me where do I modify ur source code sothat I will get "null" value for the missed data inbetween 2 sucessive delimeters??
Actually another method occured to me, but it also requires a later version of Java. I would recommend 1.4 for better String handling functionality.
My idea was to use replaceAll and preprocess all "::::" to ":: ::" and running trim on each field. This would work as long as trailing and leading blanks would not be used; you can adjust this approach by using a bogus flag character such as "|" and if ("|".equals(field)) field="";
However, not all hope is lost. There are a lot of posted versions of replaceAll type methods on the net, that people were using with earlier versions of the SDK, so you can add that to your code, and you should have a solution.
StringTokenizer treats any string that is given as delimiter argument as a list of one-char-delimiters. So "::" is actually two delimiters, that happen to be equal. Splitting a string at "::" with the StringTokenizer works only if the pattern string+"::"+string+"::" applies regularly and no string is empty. in case a string is zero or one colon is missing you won't get the results you expected.
for good error handling you should: - either iterate through the characters of the string by yourself and check for double colons, if you want to stick with 1.3 and use no additional package, - or switch to 1.4 and use the java.util.regex package, - or use the OROMatcher package from http://www.savarese.org/oro/software/OROMatcher1.1.html, - or use a parser generator like ANTLR, which will give you even more power if you happen to need more sophisticated parsing.