Thread overview | |||||
---|---|---|---|---|---|
|
September 16, 2017 How to use the Among method with a string array | ||||
---|---|---|---|---|
| ||||
Hi all, sorry for the very simple question, but I'm looking at the "among" function in std.comparison: https://dlang.org/phobos/std_algorithm_comparison.html#among If I have a string array: string[] fruit = ["apple", "pear", "strawberry"]; How do I use "among" to see if "apple" is present. E.g. I want to do this: if ("apple".among(fruit)) However that wont compile - I'm missing something fundamental in my knowledge. It seems fruit has to be a Tuple? Is there a way to use a string array instead? |
September 16, 2017 Re: How to use the Among method with a string array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrew Chapman | On Saturday, 16 September 2017 at 11:18:45 UTC, Andrew Chapman wrote:
> string[] fruit = ["apple", "pear", "strawberry"];
>
> How do I use "among" to see if "apple" is present. E.g. I want to do this:
>
> if ("apple".among(fruit))
among doesn't take an array, but rather a list of arguments:
"bar".among("foo", "bar", "baz")
For searching in an array, you can use canFind:
if(fruit.canFind("apple"))
|
September 16, 2017 Re: How to use the Among method with a string array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Saturday, 16 September 2017 at 12:09:28 UTC, Adam D. Ruppe wrote:
> On Saturday, 16 September 2017 at 11:18:45 UTC, Andrew Chapman wrote:
>> string[] fruit = ["apple", "pear", "strawberry"];
>>
>> How do I use "among" to see if "apple" is present. E.g. I want to do this:
>>
>> if ("apple".among(fruit))
>
> among doesn't take an array, but rather a list of arguments:
>
> "bar".among("foo", "bar", "baz")
>
>
> For searching in an array, you can use canFind:
>
> if(fruit.canFind("apple"))
Thanks Adam!
|
Copyright © 1999-2021 by the D Language Foundation