正如您所发现的,返回值
filter
不是数组,而是自定义范围。
滤波器
的返回值实际上是一个延迟范围,因此如果只使用前几个项,则只计算这些项。要将延迟范围转换为数组,需要使用
std.array.array
:
import std.array : array;
alllist = alllist.filter!(a => a.indexOf(str) >= 0).array;
import std.stdio;
import std.string;
import std.file : readText;
import std.algorithm;
import std.array;
void main() {
string[] srchitems = ["second", "pen"];
auto alltext = readText("testing.txt");
auto alllist = alltext.split("\n");
auto results = alllist.filter!(a => srchitems.any!(b => a.indexOf(b) >= 0));
writeln(results);
}
在上面的代码中,我们使用
滤波器