StringTokenizer Vs String.split
Posted on 2004-08-26
Till date my perception is StringTokenizer is slower than String.split();
Is that correct??
Here is a sequence of methods that need to be executed in sequence for splitting the String
In String class
String.split(regex) calls String.split(regex,limit)
String.split(regex,limit) calls Pattern.compile(regex).split(this, limit)
In pattern class
Pattern.comile(regex) calls new Pattern(regex, flag)
in turn this constructor calls compile() method for creating an Instance of Pattern
Now in split method of pattern class, it created Matcher instance and until Matcher.find() it is doing normal split logic using string.substring()
So for simple splitting, It created an instance of Pattern and matcher and to get final result it need to pass all the above methods.
whereas In StringTokenizer it just has 3 calls internally and ofcourse hasMoreTokens() and nextToken()
scanToken() and string.substring() and setMaxDelimChar() and it is not creating any other class instances also like one it has done with string.split();
Constructior calls setMaxDelimChar
nextToken calls scanToken and string.substring()
So I would like to know which do you think is the better one.