Find a char among string (string.findAmong.char)

rassoc rassoc at rassoc.org
Wed Jul 7 13:10:41 UTC 2021


On Wednesday, 7 July 2021 at 12:22:11 UTC, BoQsc wrote:
> I think nested foreach loops are more readable.
>
> ```
> import std;
> void main()
> {
>     alias alphabet = letters;
>     char[26] letters = ['a','b', 'c', 'd', 'e',
>                         'f', 'g', 'h', 'i', 'j',
>                         'k', 'l', 'm', 'n', 'o',
>                         'p', 'q', 'r', 's', 't',
>                         'u', 'v', 'w', 'x', 'y', 'z'];
>
>     string wordExample = "Book.";
> 	foreach (letter; wordExample){
>         foreach (alphabetLetter; letters){
>             if (letter == alphabetLetter) {
>             	writeln(letter);
>             }
>         }
>     	
>     }
> 						
> }
> ```

Use whatever fits your style best or what you are most familar 
with. But be aware that there's a concise alternative:

```d
import std;
void main()
{
     import std.ascii; // because there's also std.uni.isLower
     "Book.".filter!isLower.each!writeln;	
}
```

or alternatively with fully-qualified naming:

```d
import std;
void main()
{
    "Book.".filter!(std.ascii.isLower).each!writeln;						
}
```


More information about the Digitalmars-d-learn mailing list