Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

string.split returns a string[] I want a List<string> is there a one liner to convert an array to a list?

Lists in C# have the .ToArray() method. I want the inverse, where an array is transformed into a list. I know how to create a list and loop through it but I would like a one liner to swap it back.

I am using the String.Split method in the .NET 2.0 environment, so LINQ, etc. is not available to me.

like image 433
minty Avatar asked Oct 30 '08 22:10

minty


People also ask

Does split return a string?

The split() method does not change the value of the original string. If the delimiter is an empty string, the split() method will return an array of elements, one element for each character of string. If you specify an empty string for string, the split() method will return an empty string and not an array of strings.

Can you split a string array?

The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.

How do I split a string into string?

Use the Split method when the substrings you want are separated by a known delimiting character (or characters). Regular expressions are useful when the string conforms to a fixed pattern. Use the IndexOf and Substring methods in conjunction when you don't want to extract all of the substrings in a string.


2 Answers

string s = ... new List<string>(s.Split(....)); 
like image 111
Ovidiu Pacurar Avatar answered Sep 18 '22 21:09

Ovidiu Pacurar


In .Net 3.5, the System.Linq namespace includes an extension method called ToList<>().

like image 35
Max Lybbert Avatar answered Sep 21 '22 21:09

Max Lybbert