Find a char among string (string.findAmong.char)
BoQsc
vaidas.boqsc at gmail.com
Mon Jul 5 19:34:14 UTC 2021
On Monday, 5 July 2021 at 19:25:23 UTC, jfondren wrote:
> On Monday, 5 July 2021 at 19:19:19 UTC, BoQsc wrote:
>> If I use `[letter].findAmong(alphabet)` in my code, it
>> considers a dot (.) punctuation character as a letter.
>> You can see it here:
>> https://run.dlang.io/is/YWmaXU
>
> It returns a zero-length array that, because it's not null, is
> true. That's why I used .length in my example.
>
> ```
> $ rdmd --eval 'writeln("str"[$..$].length); writeln("str"[$..$]
> ? true : false)'
> 0
> true
> $ rdmd --eval 'writeln([].length); writeln([] ? true : false)'
> 0
> false
> ```
Oh alright I think I fixed it with your guidance.
```
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){
if (([letter].findAmong(alphabet)).length){
write(letter);
write(" letter is found among the alphabet.");
writeln;
}
}
}
```
But I really don't like how it looks less readable and makes less
sense on first look.
`if (([letter].findAmong(alphabet)).length)`
I'd like to use some method on the `letter` instead of []
And `.length` does not make a lot of sense when reading like an
english sentence.
More information about the Digitalmars-d-learn
mailing list