-1

I am processing data in a datagrid and want to filter out any rows which do not contain specific text. Is it possible to do this? Code below is where I am reading the data. I don't want to read/process lines which do not contain the word "INTEREST"

while (fileReader.Peek() != -1)
                        {
                            fileRow = fileReader.ReadLine();
                            fileRow = fileRow.Replace("\"", "");
                   //       fileRow = fileRow.Replace("-", "");
                            fileDataField = fileRow.Split(delimiter);
                            fileDataField = fileRow.Split(',');
                            gridLGTCash.Rows.Add(fileDataField);
                        }
                        fileReader.Close();
Ryan80
  • 31
  • 1
  • 3
  • 3
    I hope you don't think that `fileRow.Split(',')` parses a CSV row, [because it doesn't](https://stackoverflow.com/questions/2081418/). – Dour High Arch Dec 21 '18 at 00:34
  • 3
    Trust me: just use a CSV parser. You'll spare yourself *so* many headaches down the line! – vzwick Dec 21 '18 at 00:51
  • @DourHighArch Using or not a library to read a basic CSV is irrelevant. The only question is whether the OP is asking how to use an `if-statement` – Camilo Terevinto Dec 21 '18 at 00:55

3 Answers3

0

If you want to skip all lines that contain "INTEREST" check for that string using Contains:

while (fileReader.Peek() != -1)
{
  fileRow = fileReader.ReadLine();
  if (!fileRow.Contains("INTEREST"))  //<--- Add a test here
  {
    fileRow = fileRow.Replace("\"", "");
    //       fileRow = fileRow.Replace("-", "");
    fileDataField = fileRow.Split(delimiter);
    fileDataField = fileRow.Split(',');
    gridLGTCash.Rows.Add(fileDataField);
  }
}
fileReader.Close();
MikeH
  • 4,242
  • 1
  • 17
  • 32
0

Here is my fix for no record or blank record on a row. Blank row in cvs looks like this [,,,,,,,..].

using System.Text.RegularExpressions;
:
:
do {
     fileRow = fileReader.ReadLine();
     if (!Regex.IsMatch(fileRow, @"^,*$")) 
     {
        fileRow = fileRow.Replace("\"", "");
        //       fileRow = fileRow.Replace("-", "");
        fileDataField = fileRow.Split(delimiter);
        fileDataField = fileRow.Split(',');
        gridLGTCash.Rows.Add(fileDataField);
      }
  }while (fileReader.Peek() != -1)
fileReader.Close();
0

First of all, you'll be much better off with a dedicated CSV reader. string.Split() performs poorly and fails for all kinds of edge cases. There are three (at least) built into the framework, but you can easily get other (imo better) options via NuGet. That will likely side-step this whole issue for you.

Assuming for a moment you can't do that, I wouldn't use Peek() for this, either. Use the File.ReadLines() method (note: this is not the same as File.ReadAllLines(), which can be a memory hog):

var lines = File.ReadLines("filename here"))
                .Select(line => line.Replace("\"", "").Split(','))
                .Where(line => line.Any(field => field.Trim().Length > 0));
gridLGTCash.Rows.AddRange(lines.ToArray());
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794