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 07:11:56 PST 2015


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


More information about the Digitalmars-d-learn mailing list