iteration over a string
Diggory
diggsey at googlemail.com
Tue May 28 01:04:32 PDT 2013
Most algorithms for strings need the offset rather than the
character index, so:
foreach (i; dchar c; str)
Gives the offset into the string for "i"
If you really need the character index just count it:
int charIndex = 0;
foreach (dchar c; str) {
// ...
++charIndex;
}
If strings were treated specially so that they looked like arrays
of dchars but used UTF-8 internally it would hide all sorts of
performance costs. Random access into a UTF-8 string by the
character index is O(n) whereas index by the offset is O(1).
If you are using random access by character index heavily you
should therefore convert to a dstring first and then you can get
the O(1) random access time.
More information about the Digitalmars-d-learn
mailing list