std.algorithm.canFind behavior difference between arrays and elements

Steven Schveighoffer schveiguy at gmail.com
Fri Dec 7 19:04:45 UTC 2018


On 12/7/18 1:57 PM, Dennis wrote:
> On Friday, 7 December 2018 at 18:51:27 UTC, Arun Chandrasekaran wrote:
>> Why is there a difference in the behavior?
> 
> Your first assert expression is looking for a string in a larger string, 
> your second expression looks for hay which is not a string but a 
> string[]. To flatten the array, use:
> 
> assert(canFind(hay.join, "111"));

1. Use joiner, not join, as this creates a temporary array and 
immediately throws it away (wasteful)
2. It's not exactly as simple as that, because it will find "111" that 
spans 2 elements (unless that's what you want).

You'd probably want to use some kind of delimiter, like:

assert(canFind(hay.joiner("\0"), "111"));

-Steve


More information about the Digitalmars-d-learn mailing list