Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Faster split than String.split() and StringTokenizer?

416044Sep 8 2006 — edited Sep 26 2006
First I imrpoved performance of split by replacing the String.split() call with a custom method using StringTokenizer:
                final StringTokenizer st = new StringTokenizer(string, separator, true);
                String token = null;
                String lastToken = separator; //if first token is separator
                while (st.hasMoreTokens()) {
                    token = st.nextToken();
                    if (token.equals(separator)) {
                        if (lastToken.equals(separator)) { //no value between 2 separators?
                            result.add(emptyStrings ? "" : null);
                        }
                    } else {
                        result.add(token);
                    }
                    lastToken = token;
                }//next token
But this is still not very fast (as it is one of the "hot spots" in my profiling sessions). I wonder if it can go still faster to split strings with ";" as the delimiter?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 24 2006
Added on Sep 8 2006
48 comments
5,068 views