字符串生成器与列表

buz*_*jay 0 c# string stringbuilder list visual-studio-2010

我正在读取数百万行的多个文件,并且正在创建具有特定问题的所有行号的列表。例如,如果特定字段保留为空白或包含无效值。

因此,我的问题是,跟踪一百万行以上的数字列表最有效的日期类型是什么。使用字符串生成器,列表或其他方法会更有效吗?

我的最终目标是发出类似“特定字段在1-32、40、45、47、49-51等上为空白的消息。因此对于String Builder,我将检查先前的值以及是否检查它是仅多1个,我会将其从1更改为1-2,如果超过一个,则将其用逗号分隔。使用列表,我只需将每个数字添加到列表中,然后合并,一旦文件包含已被完全阅读,但是在这种情况下,我可能会有多个包含数百万个数字的列表。

这是我正在使用String Builder组合数字列表的当前代码:

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++;
    }
}   
Run Code Online (Sandbox Code Playgroud)

Ode*_*ded 5

我的最终目标是发出这样的消息:“特定字段在1-32、40、45、47、49-51上为空白

如果那是最终目标,那么通过a这样的中介表示毫无意义List<int>-只需加上a即可StringBuilder。您将以这种方式节省内存和CPU。