How to loop through characters of a string in D language?
kdevel
kdevel at vogtner.de
Thu Dec 9 18:00:42 UTC 2021
On Wednesday, 8 December 2021 at 13:01:32 UTC, BoQsc wrote:
[...]
> I'm not getting used to the syntax and that leads to poor
> readability.
It depends on what you expect when you read source code. I don't
want to read how seats in the memory are assigned to bits and
bytes. Instead I want to read what is done.
> But that might be just me.
Unfortunately not.
> Anyways,
> Here is what I've come up with.
>
> ```
> import std.stdio;
>
> void main()
> {
> string a = "abc;def;ab";
> string b;
>
> for(int i=0; i<a.length; i++){
> write(i);
> writeln(a[i]);
> if (a[i] != ';'){
> b ~= a[i];
> }
>
> }
>
> writeln(b);
> }
> ```
PRO:
- saves two lines of boilerplate code
CONS:
- raw loop
- postinc ++ is only permitted in ++C
- inconsistent spacing around "="
- mixing tabs and spaces for indentation
- arrow code
More information about the Digitalmars-d-learn
mailing list