我正在读取包含数百万行的多个文件,并且正在创建具有特定问题的所有行号的列表。例如,如果特定字段留空或包含无效值。
所以我的问题是,跟踪可能超过一百万行的数字列表的最有效的日期类型是什么。使用字符串生成器、列表或其他东西会更有效吗?
我的最终目标是输出一条消息,例如“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++;
}
}
我的最终目标是输出类似“1-32、40、45、47、49-51 上的特定字段为空白”的消息
如果这是最终目标,那么通过中间代表(例如List<int>
- 只需选择一个StringBuilder
。这样您就可以节省内存和 CPU。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)