Why do the same work about 'IndexOfAny' and 'indexOf' function?

John Colvin via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jan 7 08:22:07 PST 2015


On Wednesday, 7 January 2015 at 15:57:18 UTC, FrankLike wrote:
> On Wednesday, 7 January 2015 at 15:11:57 UTC, John Colvin wrote:
>> On Wednesday, 7 January 2015 at 14:54:51 UTC, FrankLike wrote:
>>> I want to know whether the string strs contains 
>>> 'exe','dll','a','lib',in c#,
>>> I can do : int index =  
>>> indexofany(strs,["exe","dll","a","lib"]);
>>> but in D:  I must to do like this:
>>>
>>> findStr(strs,["exe","lib","dll","a"]))
>>>
>>> bool findStr(string strIn,string[] strFind)
>>> {
>>> 	bool bFind = false;
>>> 	foreach(str;strFind)
>>> 	{
>>> 		if(strIn.indexOf(str) !=-1)
>>>              {
>>>                    bFind = true;
>>> 			break;
>>>              }
>>> 	}
>>> 	return bFind;
>>> }
>>>
>>> phobos 's string.d can add this some function to let the 
>>> indexOfAny to better?
>>>
>>> Thank you.
>>>
>>> Frank
>>
>> std.algorithm.canFind will do what you want, including telling 
>> you which of ["exe","lib","dll","a"] was found.
>>
>> If you need to know where in strs it was found as well, you 
>> can use std.algorithm.find
>
> Sorry, 'std.algorithm.find' do this work:Finds an individual 
> element in an input range,and it's Parameters: InputRange 
> haystack The range searched in.
> Element needle The element searched for.

std.algorithm.find has several overloads, one of which takes 
multiple needles. The same is true for std.algorithm.canFind

Quoting from the relevant std.algorithm.find overload docs: 
"Finds two or more needles into a haystack."


More information about the Digitalmars-d-learn mailing list