我正在阅读数百万行的多个文件,并且创建了一个包含特定问题的所有行号的列表。例如,如果特定字段留空或包含无效值。字符串生成器vs列表
所以我的问题是什么是最有效的日期类型跟踪号码的清单,可能是向上百万的行数的。会使用字符串生成器,列表或其他更有效的?
我的最终目标是要出把像“特定字段的消息是1-32,40,45,47,49-51,等等。所以在字符串构建的情况下,我会检查以前的空白值,如果它只有1个,我会将它从1更改为1-2,如果它不止一个,则用逗号分隔它。使用列表,我只需将每个数字添加到列表中,然后将它们组合一旦该文件已被完全读取然而,在这种情况下,我可能有一个包含数以百万计的数字多列表
这里是我使用的结合使用字符串构建号码列表当前代码:
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++;
}
}
[将列表转换为数字范围字符串](http://stackoverflow.com/questions/7688881/convert-list-to-number-range-string) – 2012-04-09 16:51:35