public static IEnumerable<string> ReadLines(this string text,bool skipEmpty)
{
using (System.IO.StringReader sr = new System.IO.StringReader(text))
{
while (sr.Peek() > -1)
{
string line = sr.ReadLine();
if(skipEmpty == false)
{
yield return line;
}
else if (skipEmpty && line.Length > 0)
{
yield return line;
}
}
}
}
こういう拡張メソッドを用意して居おくと便利です。
テキストを改行による行に変換してくれます。
それを使って、
IEnumerable<string[]> 空白行区切り(string text)
{
List<string> lines = new List<string>();
foreach(var line in text.ReadLines(false))
{
if(line.Any())
{
lines.Add(line);
}
else
{
yield return lines.ToArray();
lines = new List<string>();
}
}
if(lines.Any())
{
yield return lines.ToArray();
}
}
こんな感じに書けばいいのかな。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/09/22 14:17