Skip to main content
Scanner is very slow.; added 64 characters in body
Source Link
Zarkonnen
  • 22.6k
  • 14
  • 70
  • 82

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<String>, as it happens:

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

gives:

dog cat 

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

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

gives:

dog cat 

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<String>, as it happens:

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

gives:

dog cat 
Source Link
Zarkonnen
  • 22.6k
  • 14
  • 70
  • 82

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

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

gives:

dog cat