我正在阅读数以百万行计的多个文件,并创建一个包含特定问题的所有行号的列表。例如,如果特定字段为空或包含无效值。
所以我的问题是,保持一百万行数量的数字列表的最有效果的数据类型是什么?使用字符串生成器、列表或其他什么东西更有效?
我的最终目标是输出像“第 1-32,40,45,47,49-51 行出现了空白的字段”这样的消息。因此,在使用字符串生成器时,我会检查前一个值,如果只比当前值大1,则将它从 1 改为 1-2,如果大于 1 则用逗号分隔。对于列表,我只需将每个数字添加到列表中,然后在文件完全读取后将它们组合起来。但是在这种情况下,可能会有包含数百万个数字的多个列表。
这是我使用字符串生成器组合数字列表的当前代码:
string currentLine = sbCurrentLineNumbers.ToString();
string currentLineSub;
StringBuilder subCurrentLine = new StringBuilder();
StringBuilder subCurrentLineSub = new StringBuilder();
int indexLastSpace = currentLine.LastIndexOf(' ');
int indexLastDash = currentLine.LastIndexOf('-');
int currentStringInt = 0;
if (sbCurrentLineNumbers.Length == 0)
{
sbCurrentLineNumbers.Append(lineCount);
}
else if (indexLastSpace == -1 && indexLastDash == -1)
{
currentStringInt = Convert.ToInt32(currentLine);
if (currentStringInt == lineCount - 1)
sbCurrentLineNumbers.Append("-" + lineCount);
else
{
sbCurrentLineNumbers.Append(", " + lineCount);
commaCounter++;
}
}
else if (indexLastSpace > indexLastDash)
{
currentLineSub = currentLine.Substring(indexLastSpace);
currentStringInt = Convert.ToInt32(currentLineSub);
if (currentStringInt == lineCount - 1)
sbCurrentLineNumbers.Append("-" + lineCount);
else
{
sbCurrentLineNumbers.Append(", " + lineCount);
commaCounter++;
}
}
else if (indexLastSpace < indexLastDash)
{
currentLineSub = currentLine.Substring(indexLastDash + 1);
currentStringInt = Convert.ToInt32(currentLineSub);
string charOld = currentLineSub;
string charNew = lineCount.ToString();
if (currentStringInt == lineCount - 1)
sbCurrentLineNumbers.Replace(charOld, charNew);
else
{
sbCurrentLineNumbers.Append(", " + lineCount);
commaCounter++;
}
}