我正在閱讀數百萬行的多個文件,並且創建了一個包含特定問題的所有行號的列表。例如,如果特定字段留空或包含無效值。字符串生成器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