Replicating String.split with StringTokenizer

后端 未结 9 1067
心在旅途
心在旅途 2021-02-06 14:03

Encouraged by this, and the fact I have billions of string to parse, I tried to modify my code to accept StringTokenizer instead of String[]

The only t

9条回答
  •  星月不相逢
    2021-02-06 14:35

    Note: Having done some quick benchmarks, Scanner turns out to be about four times slower than String.split. Hence, do not use Scanner.

    (I'm leaving the post up to record the fact that Scanner is a bad idea in this case. (Read as: do not downvote me for suggesting Scanner, please...))

    Assuming you are using Java 1.5 or higher, try Scanner, which implements Iterator, as it happens:

    Scanner sc = new Scanner("dog,,cat");
    sc.useDelimiter(",");
    while (sc.hasNext()) {
        System.out.println(sc.next());
    }
    

    gives:

    dog
    
    cat
    

提交回复
热议问题