-3

I have a problem like this.What does StringTokenizer do than split method for white spaces in java. According to Class StringTokenizer how is it better to use split method instead of using StringTokenizer.

Sajith Herath
  • 815
  • 2
  • 9
  • 23
  • I don't think an answer can be given unless we know what sort of string manipulation problem you have in mind. So, what are you planning to do? – Tim Biegeleisen Apr 22 '17 at 06:19
  • Well, [Google](https://www.google.com.au/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#safe=off&q=stringtokenizer+vs+split) seems like a good place to start – MadProgrammer Apr 22 '17 at 06:24
  • Possible duplicate [Scanner vs. StringTokenizer vs. String.Split](http://stackoverflow.com/questions/691184/scanner-vs-stringtokenizer-vs-string-split), [Difference between using StringTokenizer and String.split( )?](http://stackoverflow.com/questions/19677919/difference-between-using-stringtokenizer-and-string-split), [Performance of StringTokenizer class vs. split method in Java](http://stackoverflow.com/questions/5965767/performance-of-stringtokenizer-class-vs-split-method-in-java) – MadProgrammer Apr 22 '17 at 06:25
  • I want to split a string variable from white spaces. According to Java documentation we can achieve that from either split method or StringTokenizer. My question is how to differ split method and StringTokenizer. Why there are two methods to achieve same thing? – Sajith Herath Apr 22 '17 at 06:32

3 Answers3

1

What does StringTokenizer do than split method for white spaces in java.

It provides backwards compatibility ... for old Java code that was developed before String.split was implemented; i.e. prior to Java 1.4.

You don't have to use it. In fact you are recommended not to use it. However, they can't / won't remove it because removing it would break old code.

(So far, the class is not officially deprecated. There is no definite harm in using it rather than the more modern ways of splitting strings, so (IMO) deprecation is unlikely.)

Stephen C
  • 632,615
  • 86
  • 730
  • 1,096
0

As you can read in the class description of your own link

"StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead."

Henning Luther
  • 1,991
  • 10
  • 21
0

Quote from the linked API doc:

StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

Harald Gliebe
  • 5,962
  • 2
  • 26
  • 36