In my understanding the StringTokenizer divides the String in tokens which on printing produces same result as split function depending on the delimiter. For example, consider this code:
String str="This is a text."; StringTokenizer st=new StringTokenizer(str); while(st.hasMoreTokens()) { System.out.println(st.nextToken()); } And this:
String str="This is a text."; String st[]=str.split(" "); for(int i=0;i<st.length;i++) { System.out.println(st[i]); } In both the cases the output is:
This is a text. Then why should I use StringTokenizer when I can achieve same thing with split function?
StringTokenizer. Its Javadoc claims it is obsolete.StringTokenizeris that it will return which characters it splits on.String.split()doesn't, it just eats the delimiters.