Java's Scanner vs String.split() vs StringTokenizer; which should I use?

后端 未结 5 1965
一个人的身影
一个人的身影 2020-12-05 21:11

I am currently using split() to scan through a file where each line has number of strings delimited by \'~\'. I read somewhere that Scanner

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-05 21:32

    You don't actually need a regex here, because you are splitting on a fixed string. Apache StringUtils split does splitting on plain strings.

    For high volume splits, where the splitting is the bottleneck, rather than say file IO, I've found this to be up to 10 times faster than String.split(). However, I did not test it against a compiled regex.

    Guava also has a splitter, implemented in a more OO way, but I found it was significantly slower than StringUtils for high volume splits.

提交回复
热议问题