Thread overview | |||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
January 07, 2015 Why do the same work about 'IndexOfAny' and 'indexOf' function? | ||||
---|---|---|---|---|
| ||||
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 |
January 07, 2015 Re: Why do the same work about 'IndexOfAny' and 'indexOf' function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to FrankLike | 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
|
January 07, 2015 Re: Why do the same work about 'IndexOfAny' and 'indexOf' function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Colvin | 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.
|
January 07, 2015 Re: Why do the same work about 'IndexOfAny' and 'indexOf' function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to FrankLike | FrankLike: > 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"]. Seems this: http://rosettacode.org/wiki/File_extension_is_in_extensions_list#D Bye, bearophile |
January 07, 2015 Re: Why do the same work about 'IndexOfAny' and 'indexOf' function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Wednesday, 7 January 2015 at 16:02:25 UTC, bearophile wrote:
> FrankLike:
>
>> 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"].
>
> Seems this:
> http://rosettacode.org/wiki/File_extension_is_in_extensions_list#D
>
> Bye,
> bearophile
Which uses this overload:
size_t canFind(Range, Ranges...)(Range haystack, Ranges needles)
|
January 07, 2015 Re: Why do the same work about 'IndexOfAny' and 'indexOf' function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to FrankLike | 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."
|
January 07, 2015 Re: Why do the same work about 'IndexOfAny' and 'indexOf' function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Colvin | > 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." string strs ="hello.exe"; string[] s =["lib","exe","a","dll"]; auto a = canFind!(string,string[])(strs,s); writeln("a is ",a); string strsb ="hello."; auto b = canFind!(string,string[])(strsb,s); writeln("b is ",b); Get error: does not match template declaration canFind(alias pred = "a ==b") you can test it. Thank you. |
January 07, 2015 Re: Why do the same work about 'IndexOfAny' and 'indexOf' function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to FrankLike | Try this: http://dlang.org/phobos-prerelease/std_algorithm#.findAmong T -- MACINTOSH: Most Applications Crash, If Not, The Operating System Hangs |
January 08, 2015 Re: Why do the same work about 'IndexOfAny' and 'indexOf' function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | On Wednesday, 7 January 2015 at 17:08:55 UTC, H. S. Teoh via Digitalmars-d-learn wrote: > Try this: > > http://dlang.org/phobos-prerelease/std_algorithm#.findAmong > > > T You mean ? The result is not that I want to get! ---------------test.d-------------- import std.stdio, std.algorithm,std.string; auto ext =["exe","lib","a","dll"]; auto strs = "hello.exe"; void main() { auto b = findAmong(ext,strs); writeln("b is ",b); } ---------result----- b is ["exe","lib","a","dll"] -------------------- note: 1. I only want to find the given string 'hello.exe' whether to include any a string in the ["exe","lib","a","dll"]. 2. I think the 'indexOfAny' function of string.d do the same work with 'indexOf',This is not as it should be. Frank |
January 08, 2015 Re: Why do the same work about 'IndexOfAny' and 'indexOf' function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | On Wednesday, 7 January 2015 at 17:08:55 UTC, H. S. Teoh via Digitalmars-d-learn wrote: > Try this: > > http://dlang.org/phobos-prerelease/std_algorithm#.findAmong > > > T Thank you,it can work. but it's not what I want. ---------------test.d-------------- import std.stdio, std.algorithm,std.string; auto ext =["exe","lib","a","dll"]; auto strs = "hello.dll"; void main() { auto b = findAmong(ext,strs); writeln("b is ",b); } ---------result----- b is ["dll"] -------------------- I think if 'indexOfAny' function of string.d do the work ,it should be ok. such as : auto b = "hello.dll".indexOfAny(["exe","lib","a","dll"]); writeln("b is ",b); The result should be 'true',if it can work. Can you suggest 'phobos' to update 'indexOfAny' fuction? Thank you. Frank |
Copyright © 1999-2021 by the D Language Foundation