endsWith - for a string vs an array of strings
bearophile via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Jan 10 12:58:51 PST 2015
Laeeth Isharc:
> I understand from previous discussion there is some difficulty
> over immutability. I did not quite figure out what the
> solution was in this case:
>
> import std.array;
> import std.string;
> import std.stdio;
> void main(string[] args)
> {
> string[] test=["1","two","three!"];
> auto a="arghtwo".endsWith(test);
> writefln("%s",a);
> }
>
> This does not compile...
Take a look at your error messages:
> std.algorithm.endsWith(alias pred = "a == b", Range,
> Needles...)(Range doesThisEnd, Needles withOneOfThese)
Needles is not an array type, it's a type tuple, so
withOneOfThese doesn't accept an array of strings.
This is correct:
auto a = "arghtwo".endsWith("1","two","three!");
In D there is a feature that allows a function to accept both an
array of items and items, but it's not used here by endsWith, for
reasons I don't understand (other people can answer this).
So if you really want to pack the strings in some kind of unity,
you can do this as workaround:
void main() {
import std.stdio, std.array, std.string, std.typetuple;
alias test = TypeTuple!("1", "two", "three!");
auto b = "arghtwo".endsWith(test[]);
b.writeln;
}
Bye,
bearophile
More information about the Digitalmars-d-learn
mailing list