Tuesday, 15 March 2016

C# Code Snippets

Return number only from text
private string GetNumbers(string input)
{
    return new string(input.Where(c => char.IsDigit(c)).ToArray());
}


Read CSV file

static List ReadCSV(string filename)
{
  return File.ReadLines(filename)
          //.Skip(1)
          .Where(s => s != "")
          .Select(s => s.Split(new[] { ',' }))
          .Select(a => new Result
           {
              OrgFN = a[0],
              NewFN = a[2],
              Barcode = a[1]
           })
           .ToList();
}



Or can use this:
http://www.codeproject.com/Articles/12170/FileHelpers-v-Delimited-CSV-or-Fixed-Data-Impor

No comments:

Post a Comment