Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple string iterator in Haxe
//Simple string iterator for 'for' loops
//Based on the work by Mark Knol, modifed by SheatNoisette.
//https://github.com/HaxeFoundation/code-cookbook
//Creative Common By Attribution 4.0
class StringIterator {
var input:String;
var step:Int;
var index:Int;
var end:Int;
public inline function new (input:String, ?step:Int = 1) {
this.input = input;
this.step = step;
this.index = 0;
this.end = input.length;
}
public inline function hasNext() {
return index < end;
}
public inline function next() {
return input.charAt((index += step) - step);
}
}
/*
Usage:
for (i in new StringIterator("Hey!")) {
trace(i);
}
- Out:
H
e
y
!
for (i in new StringIterator("Hey!", 2)) {
trace(i);
}
- Out:
H
y
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.