Is there anyway we can directly access a certain(lets say 20th) element in a stringTokenizer. Every now and then I need only a certain element from it and do not need others, yet I have to traverse through all elements.
EDIT: I also want to ignore empty elements.
Am I missing something?
You could try Apache Commons Lang’s StringUtils class, which can split a string while ignoring empty elements and handling
nullstrings for you.A tokenizer would have to read at least n tokens in order to determine which is the n-th one. Thus it might be easier to just create a string array using
String#split()orStringUtils.split(...).Note that I’d prefer
StringUtils.split(...)since it doesn’t return empty elements if I don’t want them, i.e.StringUtils.split(",a,b,c;;d,e,,f",";,");would return["a","b","c","d","e","f"]whereasString#split()would return["","a","b","c","","d","e","","f"]