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.
But now I want to know in a string (like "hello.exe" or
"hello.a",or "hello.dll" or "hello.lib" ) whether contains any of
them: ["exe","dll","a","lib"].
My function 'findStr' works fine. If the string.d's function
'indexOfAny' do this work,it will happy.(but now 'IndexOfAny'
and 'indexOf' do the same work) .
Thank you.