How to split () a separator string into List <String>

I had this code:

String[] lineElements; . . . try { using (StreamReader sr = new StreamReader("TestFile.txt")) { String line; while ((line = sr.ReadLine()) != null) { lineElements = line.Split(','); . . . 

but then I thought that I should go with the list. But this code:

  List<String> listStrLineElements; . . . try { using (StreamReader sr = new StreamReader("TestFile.txt")) { String line; while ((line = sr.ReadLine()) != null) { listStrLineElements = line.Split(','); . . . 

... gives me: "It is not possible to implicitly convert the type 'string []' to 'System.Collections.Generic.List'"

+75
arraylist list c #
Feb 13 2018-12-12T00:
source share
7 answers

string.Split() returns an array - you can convert it to a list using ToList() :

 listStrLineElements = line.Split(',').ToList(); 
+165
Feb 13 2018-12-12T00:
source share

Or use:

 List<string> list = new List<string>(array); 

or from LINQ:

 List<string> list = array.ToList(); 

Or change your code to not rely on a specific implementation:

 IList<string> list = array; // string[] implements IList<string> 
+45
Feb 13 2018-12-12T00:
source share

Enable use of the System.Linq namespace

 List<string> stringList = line.Split(',').ToList(); 

you can use it with ease to iterate through each element.

 foreach(string str in stringList) { } 

String.Split() returns an array, so it converts it to a list using ToList()

+8
Sep 09 '16 at 6:36
source share

Simply you can use with using System.Linq;

 List<string> stringList = line.Split(',') // this is array .ToList(); // this is a list which you can loop in all split string 
+3
Oct 24 '17 at 11:38 on
source share

try this List stringList = line.Split (','). ToList ();

+2
Dec 12 '17 at 14:21
source share
 string[] thisArray = myString.Split('/');//<string1/string2/string3/---> List<string> myList = new List<string>(); //make a new string list myList.AddRange(thisArray); 

Use AddRange to pass string[] and get a list of strings.

+1
Sep 09 '15 at 16:23
source share

This will read the csv file and includes a csv line separator that processes double quotes, and it can read even if excel has it.

  public List<Dictionary<string, string>> LoadCsvAsDictionary(string path) { var result = new List<Dictionary<string, string>>(); var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); System.IO.StreamReader file = new System.IO.StreamReader(fs); string line; int n = 0; List<string> columns = null; while ((line = file.ReadLine()) != null) { var values = SplitCsv(line); if (n == 0) { columns = values; } else { var dict = new Dictionary<string, string>(); for (int i = 0; i < columns.Count; i++) if (i < values.Count) dict.Add(columns[i], values[i]); result.Add(dict); } n++; } file.Close(); return result; } private List<string> SplitCsv(string csv) { var values = new List<string>(); int last = -1; bool inQuotes = false; int n = 0; while (n < csv.Length) { switch (csv[n]) { case '"': inQuotes = !inQuotes; break; case ',': if (!inQuotes) { values.Add(csv.Substring(last + 1, (n - last)).Trim(' ', ',')); last = n; } break; } n++; } if (last != csv.Length - 1) values.Add(csv.Substring(last + 1).Trim()); return values; } 
+1
Oct 17 '17 at 12:36 on
source share



All Articles