Comma separated string to List

The String.Split method creates an array of substrings by splitting the input string based on one or more delimiters. This method is often the easiest way to separate a string on word boundaries. It's also used to split strings on other specific characters or strings.

Note

The C# examples in this article run in the Try.NET inline code runner and playground. Select the Run button to run an example in an interactive window. Once you execute the code, you can modify it and run the modified code by selecting Run again. The modified code either runs in the interactive window or, if compilation fails, the interactive window displays all C# compiler error messages.

The following code splits a common phrase into an array of strings for each word.

string phrase = "The quick brown fox jumps over the lazy dog."; string[] words = phrase.Split(' '); foreach (var word in words) { System.Console.WriteLine($"<{word}>"); }

Every instance of a separator character produces a value in the returned array. Consecutive separator characters produce the empty string as a value in the returned array. You can see how an empty string is created in the following example, which uses the space character as a separator.

string phrase = "The quick brown fox jumps over the lazy dog."; string[] words = phrase.Split(' '); foreach (var word in words) { System.Console.WriteLine($"<{word}>"); }

This behavior makes it easier for formats like comma-separated values (CSV) files representing tabular data. Consecutive commas represent a blank column.

You can pass an optional StringSplitOptions.RemoveEmptyEntries parameter to exclude any empty strings in the returned array. For more complicated processing of the returned collection, you can use LINQ to manipulate the result sequence.

String.Split can use multiple separator characters. The following example uses spaces, commas, periods, colons, and tabs as separating characters, which are passed to Split in an array . The loop at the bottom of the code displays each of the words in the returned array.

char[] delimiterChars = { ' ', ',', '.', ':', '\t' }; string text = "one\ttwo three:four,five six seven"; System.Console.WriteLine($"Original text: '{text}'"); string[] words = text.Split(delimiterChars); System.Console.WriteLine($"{words.Length} words in text:"); foreach (var word in words) { System.Console.WriteLine($"<{word}>"); }

Consecutive instances of any separator produce the empty string in the output array:

char[] delimiterChars = { ' ', ',', '.', ':', '\t' }; string text = "one\ttwo :,five six seven"; System.Console.WriteLine($"Original text: '{text}'"); string[] words = text.Split(delimiterChars); System.Console.WriteLine($"{words.Length} words in text:"); foreach (var word in words) { System.Console.WriteLine($"<{word}>"); }

String.Split can take an array of strings (character sequences that act as separators for parsing the target string, instead of single characters).

string[] separatingStrings = { "<<", "..." }; string text = "one<See also

Apr 23, 2020by Sai gowtham

To create a comma separated string from a list string, we can use the String.join() method in C#.

Here is an example that converts myList to commaString.

using System; using System.Collections.Generic; class ConvertEnum { static void Main() { IList<string> myList = new List<string>{"Hello","1","testing","2"}; string commaString = string.Join(",", myList); System.Console.WriteLine(commaString); } }

Output:

In this demo, we are going to learn about how to rotate an image continuously using the css animations.

In this demo, i will show you how to create a instagram login page using html and css.

In this demo, i will show you how to create a pulse animation using css.

In this demo, i will show you how to create a snow fall animation using css and JavaScript.

Comma separated string to List

64,026 students enrolled

52 hours of video content

View Course

Comma separated string to List

284,472 students enrolled

40 hours of video content

View Course

Comma separated string to List

152,857 students enrolled

48.5 hours of video content

View Course

Person[] people = { new Person(){ FirstName="Steve", LastName="Jobs"}, new Person(){ FirstName="Bill", LastName="Gates"}, new Person(){ FirstName="Lary", LastName="Page"} }; var str = String.Join(",", people.Select(p => p.FirstName) );