Why do the same work about 'IndexOfAny' and 'indexOf' function?
FrankLike via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Jan 9 04:46:53 PST 2015
> be creative! ;-)
>
> import std.algorithm, std.stdio;
>
> void main () {
> string s = "he is at plane";
> if (findAmong!((string a, string b) => b.canFind(a))([s],
> ["home", "office", "sea", "plane"]).length) {
> writeln("got it!");
> } else {
> writeln("alas...");
> }
> }
>
> or:
>
> import std.algorithm, std.stdio;
>
> void main () {
> string s = "he is at home";
> if (["home", "office", "sea", "plane"].canFind!((a, string
> b) => b.canFind(a))(s)) {
> writeln("got it!");
> } else {
> writeln("alas...");
> }
> }
The code is the best,and it's better than indexOfAny in C#:
import std.algorithm, std.stdio;
void main ()
{
auto places = [ "home", "office", "sea","plane"];
auto strWhere = "He is in the sea.";
auto where = places.canFind!(a => strWhere.canFind(a));
writeln("Result is ",where);
}
More information about the Digitalmars-d-learn
mailing list