Scanner vs. StringTokenizer vs. String.Split

前端 未结 10 1281
太阳男子
太阳男子 2020-11-22 10:56

I just learned about Java\'s Scanner class and now I\'m wondering how it compares/competes with the StringTokenizer and String.Split. I know that the StringTokenizer and Str

10条回答
  •  深忆病人
    2020-11-22 12:00

    Let's start by eliminating StringTokenizer. It is getting old and doesn't even support regular expressions. Its documentation states:

    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.

    So let's throw it out right away. That leaves split() and Scanner. What's the difference between them?

    For one thing, split() simply returns an array, which makes it easy to use a foreach loop:

    for (String token : input.split("\\s+") { ... }
    

    Scanner is built more like a stream:

    while (myScanner.hasNext()) {
        String token = myScanner.next();
        ...
    }
    

    or

    while (myScanner.hasNextDouble()) {
        double token = myScanner.nextDouble();
        ...
    }
    

    (It has a rather large API, so don't think that it's always restricted to such simple things.)

    This stream-style interface can be useful for parsing simple text files or console input, when you don't have (or can't get) all the input before starting to parse.

    Personally, the only time I can remember using Scanner is for school projects, when I had to get user input from the command line. It makes that sort of operation easy. But if I have a String that I want to split up, it's almost a no-brainer to go with split().

提交回复
热议问题