问题描述:

How can I add additional 30 character from the char that matches where in my code below.

private void CheckGarbageCharacters(string input)

{

var contentList = File.ReadAllLines(input).ToList();

int[] lineno = { 0 };

foreach (var line in contentList)

{

lineno[0]++;

foreach (var errorModel in from c in line

where c > '\u007F'

select new ErrorModel

{

Filename = Path.GetFileName(input),

LineNumber = lineno[0],

ErrorMessage = "Garbage character found.",

Text = c.ToString()

})

{

_errorList.Add(errorModel);

}

}

}

网友答案:

I'm not sure I fully understand your question but based on the code you have provided it seems like you are trying to do something like this...

~ Pseudo Code ~ This has not been tested ~

private void CheckGarbageCharacters(string filename)
{
    var lines = File.ReadAllLines(filename).ToList();
    for (int i = 0; i < lines.Count; i++)
    {
         var line = lines[i];
         for (int j = 0; j < line.Length; j++)
         {
              var c = line[j];
              if (c > '\u007F')
              {
                   // Grab the next 30 characters after 'c'
                   var text = c.ToString();
                   for (int k = 0; k < 30; k++)
                   {
                        if ((j + k) > (line.Length - 1))
                        {
                             break;
                        }
                        text += line[j + k].ToString();
                   }
                   // Create the error message
                   var error = new ErrorModel()
                   {
                        Filename = Path.GetFileName(filename),
                        LineNumber = i,
                        ErrorMessage = "Garbage character found.",
                        Text = text
                   };
                   // Add the error to the list
                   _errorList.Add(error);
              }
         }
    }
}

I'm not sure what you mean by "add additional 30 characters from the char that matches where in my code" though.

EDIT

I've updated my answer according to the information you've provided in the comments. I believe this is what you are trying to do here.

相关阅读:
Top