public static class Extention{
/// <summary>
/// Splits a text into lines.
/// </summary>
public static IEnumerable<string> Lines(this string text) => text.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
/// <summary>
/// Splits a text into blocks of lines. Split occurs on each empty line.
/// </summary>
public static IEnumerable<string> Blocks(this string text) => text.Trim().Split(Environment.NewLine + Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
}
代码为:
var result = text.Blocks()
.Select(b => b.Lines());
2条答案
按热度按时间p8ekf7hl1#
可以将
String.Split
与两个串联的Environment.NewLine
一起使用:https://dotnetfiddle.net/NinneE
sd2nnvve2#
按新行拆分输入是AOC的经典之作。下面是我的扩展Method.net7的一部分:
代码为:
注:
.Split(Environment.NewLine + Environment.NewLine,
为.NET 7