I was wondering what would be the fastest method to iterate over the characters in a string with Java. A small test implements the following things:
- using an Iterable
You can download the Eclipse project as a tar or zip or browse the code online here; have a look at the
StringIteratorTest class first.
Without further ado here are the results:
|Variant||Time in ms.|
If speed is what you want you shouldn’t use Iterators but one of the other solutions instead. On the other hand if you just like to play with Iterators and classes that implement
Iterable you may want to choose the slower solution.
1 thought on “Iterating over the characters in a string”
Interesting. Thanks for sharing the info.
Comments are closed.