Read each string from a string array

Polachan Paily 91 Reputation points
2022-05-08T18:28:59.93+00:00

Hi

I am trying to store each string into a variable from a string array. I have a string like

 string[]  fileContent = ftp.ReadFiles(cellValue); fileContent   array holding the values as given below

        "1","Truck","FN60HZU","'WC",,"26/03/2022","H2","KS","2","29580225","GY","KMAXD","12","12","12",,"17","5TST001","16:01:00","16:07:16","40:D3:AE:CB:16:EE"
        "2","Truck","YD17AMM","'WC",,"26/03/2022","H2","KS","1","31570225","BS","DURAVIS","11","11","11",,"17","5TST001","15:55:31","16:00:11","40:D3:AE:CB:16:EE"
        "2","Truck","YD17AMM","'WC",,"26/03/2022","H2","KS","1","31570225","BS","DURAVIS","11","11","11",,"17","5TST001","15:55:31","16:00:11","40:D3:AE:CB:16:EE"
    foreach (string s in fileContent)
    {
              string vehicleType = "Truck",
              string regno = ""FN60HZU"
             ..................................
            ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    }

How can I store each string into a variable ?
Regards

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,618 questions
{count} votes

4 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,386 Reputation points
    2022-05-08T18:58:02.993+00:00

    Consider using CsvHelper, in short you create a class/model to represent your data than read it. and if you do not want quotes use csv.Configuration.IgnoreQuotes = true;

    void Main()
    {
        using (var reader = new StreamReader("path\\to\\file.csv"))
        using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
        {
            var records = csv.GetRecords<Foo>();
        }
    }
    
    public class Foo
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
    
    0 comments No comments

  2. Polachan Paily 91 Reputation points
    2022-05-08T19:31:38.687+00:00

    Hi karenpayneoregon

    I am trying to parse the data without using csv helper. I want to get each string into a a variable from an array string

    0 comments No comments

  3. Karen Payne MVP 35,386 Reputation points
    2022-05-08T20:30:07.46+00:00

    You can get all items as shown below, here I get one part of a line into an anonymous type VehicleType is a string.

    var results = (from line in fileContent where line.Length > 0
        let Items = line.Split(',') select new { VehicleType = Items[0] }).ToList();
    
    foreach (var item in results)
    {
        Console.WriteLine(item.VehicleType);
    }
    

    As you can guess we need to do conversions if a property is not a string e.g.

    var results = (from line in fileContent where line.Length > 0
        let Items = line.Split(',') select new { VehicleType = Items[0], SomeOtherProperty = Convert.ToDateTime(Items[1]) }).ToList();
    

  4. Jack J Jun 24,491 Reputation points Microsoft Vendor
    2022-05-18T09:40:13.953+00:00

    @Polachan Paily , Welcome to Microsoft Q&A, we could create a class for the csv file.

    Then, we need to remove " " for every variable.

    Here is a code example you could refer to.

     static void Main(string[] args)  
            {  
                string[] fileContent = File.ReadAllLines(@"C:\Users\username\Desktop\t.csv").Select(i=>i.Replace("\"", "")).ToArray();  
                var result=(from line in fileContent  
                           let Items = line.Split(',')  
                           select new CsvContent   
                           {     
                               Col1 =Convert.ToInt32(Items[0]),  
                               Col2 = Items[1],  
                               Col3 = Items[2],  
                               Col4 = Items[3],  
                               Col5 = Items[4],  
                               Col6 = DateTime.ParseExact(Items[5], "dd/MM/yyyy", CultureInfo.InvariantCulture),  
                               Col7 = Items[6],  
                               Col8 = Items[7],  
                               Col9 =Convert.ToInt32( Items[8]),  
                               Col10 = Convert.ToInt32(Items[9]),  
                               Col11 = Items[10],  
                               Col12= Items[11],  
                               Col13= Convert.ToInt32(Items[12]),  
                               Col14 = Convert.ToInt32(Items[13]),  
                               Col15 = Convert.ToInt32(Items[14]),  
                               Col16 = Items[15],  
                               Col17= Convert.ToInt32(Items[16]),  
                               Col18 = Items[17],  
                               Col19= DateTime.ParseExact(Items[18], "HH:mm:ss", CultureInfo.InvariantCulture),  
                               Col20 = DateTime.ParseExact(Items[19], "HH:mm:ss", CultureInfo.InvariantCulture),  
                               Col21 = Items[20]  
      
      
                           }).ToList();  
      
      
            }  
      
            public class CsvContent  
            {  
                public int Col1 { get; set; }   
      
                public string Col2 { get; set; }  
      
                public string Col3 { get; set; }  
                public string Col4 { get; set; }  
                public string Col5 { get; set; }  
                public DateTime Col6 { get; set; }  
                public string Col7 { get; set; }  
                public string Col8 { get; set; }  
                public int Col9 { get; set; }  
                public int Col10 { get; set; }  
                public string Col11 { get; set; }  
                public string Col12 { get; set; }  
                public int Col13 { get; set; }  
                public int Col14 { get; set; }  
                public int Col15 { get; set; }  
                public string Col16 { get; set; }  
                public int Col17 { get; set; }  
                public string Col18 { get; set; }  
                public DateTime Col19 { get; set; }  
      
                public DateTime Col20 { get; set; }  
                public string Col21 { get; set; }  
      
      
      
            }  
    

    Tested result:

    203194-image.png

    Best Regards,
    Jack

    0 comments No comments