The easiest way to parse a comma-delimited string into some kind of object that I can skip to access individual values?

What is the easiest way to parse a string list of values ​​separated by commas into some object that I can skip so that I can easily access individual values?

Example line: "0, 10, 20, 30, 100, 200"

I'm a little new to C #, so forgive me for a simple question. Thank you

+53
string c #
Feb 10 '10 at 9:31
source share
6 answers

there are errors with this - but ultimately the easiest way would be to use

 string s = [yourlongstring]; string[] values = s.Split(','); 

If the number of commas and entries is not important, and you want to get rid of the "empty" values, you can use

 string[] values = s.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); 

However, one thing is that it will contain spaces before and after your lines. You can use some Linq magic to solve this problem:

 string[] values = s.Split(',').Select(sValue => sValue.Trim()).ToArray(); 

This is if you are using .Net 3.5, and you have a System.Linq declaration at the top of the source file.

+125
Feb 10 '10 at 9:36
source share
  var stringToSplit = "0, 10, 20, 30, 100, 200"; 



  // To parse your string var elements = test.Split(new[] { ',' }, System.StringSplitOptions.RemoveEmptyEntries); 



  // To Loop through foreach (string items in elements) { // enjoy } 
+20
Feb 10 '10 at 9:36
source share

Use Linq, it is a very fast and easy way.

 string mystring = "0, 10, 20, 30, 100, 200"; var query = from val in mystring.Split(',') select int.Parse(val); foreach (int num in query) { Console.WriteLine(num); } 
+7
Feb 10 '10 at 9:40
source share

The pattern matches all asymmetric characters. This will limit you to non-negative integers, but for your example, this will be more than enough.

 string input = "0, 10, 20, 30, 100, 200"; Regex.Split(input, @"\D+"); 
+4
Feb 10 '10 at
source share

I think it's better to use the Microsoft.VisualBasic.FileIO.TextFieldParser class if you work with text files separated by commas.

+3
Feb 10 2018-10-10
source share

Sometimes the columns will have commas inside themselves, for example:

Some Elements, Another Element, Also Another Element

In these cases, splitting into "," will split some columns. It may be simpler, but I just made my own method (as a bonus, it processes spaces after commas and returns IList):

 private IList<string> GetColumns(string columns) { IList<string> list = new List<string>(); if (!string.IsNullOrWhiteSpace(columns)) { if (columns[0] != '\"') { // treat as just one item list.Add(columns); } else { bool gettingItemName = true; bool justChanged = false; string itemName = string.Empty; for (int index = 1; index < columns.Length; index++) { justChanged = false; if (subIndustries[index] == '\"') { gettingItemName = !gettingItemName; justChanged = true; } if ((gettingItemName == false) && (justChanged == true)) { list.Add(itemName); itemName = string.Empty; justChanged = false; } if ((gettingItemName == true) && (justChanged == false)) { itemName += columns[index]; } } } } return list; } 
+2
Jan 20 '17 at 14:45
source share