如何用仙人掌从文件中检索空白字符串之后的字符串



我有一个文件,格式如下:

header line 1
header line 2
header line 3
line 1
line 2
line 3
...

我需要在头之后用这些行(line 1line 2line 3等(获得Iterable<String>。如何在仙人掌的帮助下实现这一点?标题由空行分隔。

我可以用以下代码跳过标题行:

new Skipped<>(
new SplitText(
new TextOf(this.path),
"n"
),
4
);

现在如何将具有跳过标头的Iterable<Text>映射到Iterable<String>

我正试图用这个代码来做这件事,但它不起作用:

new Mapped<>(
new FuncOf<>(
input -> input.asString()
),
new Skipped<>(
new SplitText(
new UncheckedText(
new TextOf(
this.path
)
),
"n"
),
4
)
);

当您调用:时

new FuncOf<>(
input -> input.asString()
)

实际上,您正在调用FuncOf<>(final Y result),这不是您想要的。

请尝试提供lambda作为Func的实现。例如,以下工作:

@Test
public void test() {
final String text =
"header line 1n" +
"header line 2n" +
"header line 3n" +
"n" +
"line 1n" +
"line 2n" +
"line 3";
MatcherAssert.assertThat(
new Mapped<>(
txt -> txt.asString(),
new Skipped<>(
new SplitText(
text,
"n"
),
4
)
),
Matchers.contains("line 1", "line 2", "line 3")
);
}

最新更新